[英]Pivot data row to header using SQL in Postgresql
我使用 SQL 请求得到这个结果:
NBR_TYPE Count
-----------------------
0021 20
0022 19
0023 13
但我想要这样的结果:
0021 0022 0023
---------------------------------
20 19 13
谁能帮助我使用 PosgreSQL 实现这一目标,谢谢。
如果您有预定义的值列表,则可以对 pivot 使用条件聚合:
select
max(count) filter(where nbr_type = '0021') "0021",
max(count) filter(where nbr_type = '0022') "0022",
max(count) filter(where nbr_type = '0023') "0023"
from mytable
这假设每个nbr_type
出现在一行中; 否则,您可能希望将max()
更改为更有意义的聚合 function,例如sum()
或avg()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.