数据仓库服务 GAUSSDB(DWS)-GaussDB(DWS)如何实现行转列及列转行?:静态行转列

时间:2024-06-21 18:00:22

静态行转列

静态行转列需要手动指定每一列的列名,如果存在则取其对应值,否则将赋其默认值0。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
SELECT name,
sum(case when subject='math' then score else 0 end) as math,
sum(case when subject='physics' then score else 0 end) as physics,
sum(case when subject='literature' then score else 0 end) as literature FROM students_info GROUP BY name;
 name | math | physics | literature
------+------+---------+------------
 matu |   75 |      90 |         85
 lily |   95 |      80 |         92
 jack |   90 |      95 |         95
(3 rows)
support.huaweicloud.com/dws_faq/dws_03_2110.html