在PostgreSQL中创建数据透视表

时间:2018-11-19 02:24:16

标签: sql postgresql cron

我有一个包含以下信息的表:

enter image description here

现在我需要像这样枢轴旋转:

enter image description here

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您可以使用条件聚合:

select hk_district,
       sum(complaint_no) filter (where month = '201801') as compaints_201801,
       sum(complaint_no) filter (where month = '201802') as compaints_201802,
       sum(complaint_no) filter (where month = '201803') as compaints_201803
from t
group by hk_district;