Vertica SQL-将列号转换为自己的列
我在CTE表中列出了以下数据(示例) 我希望,从我的主查询中,每个列都有一个新的列,该列的值为;每个ID只维护一行 我可以让它每个ID有3行,空值填补空白,但找不到解决方案,每个ID有1行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
谢谢我想你在找这样的东西
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