我需要把我的桌子换一下。现在我有了这种类型的表:
Atr_1|Atr_2
A | 1
A | 2
但是我想得到下一个结果
Atr_1|Atr_2|Atr_3
A | 1 | 2
为了达到这个结果,我应该如何转置我的表?
发布于 2019-02-05 21:13:46
使用min()
或max()
聚合的用例语句:
select Atr_1,
max(case when Atr_2=1 then 1 end ) Attr_2,
max(case when Atr_2=2 then 2 end ) Attr_3
from table t
group by Atr_1;
发布于 2019-02-05 20:26:31
如果您只有两个值,则min()
和max()
可以执行您想要的操作:
select atr_1, min(atr_2) as atr_2, max(atr_3) as atr_3
from t
group by atr_1;
发布于 2019-02-05 20:28:33
我认为您需要聚合:
SELECT atr_1,
MAX(CASE WHEN SEQ = 1 THEN atr_2 END) as atr_2,
MAX(CASE WHEN SEQ = 2 THEN atr_2 END) as atr_3
FROM (SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY atr_1 ORDER BY atr_2) AS SEQ
FROM table t
) t
GROUP BY atr_1;
https://stackoverflow.com/questions/54534388
复制相似问题