Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vertica SQL-将列号转换为自己的列_Sql_Pivot_Window Functions_Rank_Vertica - Fatal编程技术网

Vertica SQL-将列号转换为自己的列

Vertica SQL-将列号转换为自己的列,sql,pivot,window-functions,rank,vertica,Sql,Pivot,Window Functions,Rank,Vertica,我在CTE表中列出了以下数据(示例) 我希望,从我的主查询中,每个列都有一个新的列,该列的值为;每个ID只维护一行 我可以让它每个ID有3行,空值填补空白,但找不到解决方案,每个ID有1行 谢谢我想你在找这样的东西 with t_cte as ( select id, value, row_number() over(partition by id order by value) rn from mytable) select ID, max(case when rn=1 the

我在CTE表中列出了以下数据(示例)

我希望,从我的主查询中,每个列都有一个新的列,该列的值为;每个ID只维护一行

我可以让它每个ID有3行,空值填补空白,但找不到解决方案,每个ID有1行


谢谢

我想你在找这样的东西

with t_cte as (
    select id, value, row_number() over(partition by id order by value) rn
    from mytable)
select ID, max(case when rn=1 then [Value] else 0 end) Rank1,
           max(case when rn=2 then [Value] else 0 end) Rank2,
           max(case when rn=3 then [Value] else 0 end) Rank3
from t_cte
group by ID;

我认为您需要条件聚合:

select
    id,
    max(case when rn = 1 then value end) value1,
    max(case when rn = 2 then value end) value2,
    max(case when rn = 3 then value end) value3
from (
    select id, value, row_number() over(partition by id order by value) rn
    from mytable
) t
group by id