我需要旋转表并获取每年存在的受尊敬ID的计数

时间:2019-02-05 14:35:10

标签: postgresql crosstab

我需要按年份检索数据,即年份的count(values),并且格式应为year和count(values)

select * 
from crosstab(select id, year, count(values) from table)
as res(id int, year1 int, year2 int, year3 int, year4 int)

表格中的数据

============================
Id | Year        | values
============================
1  | 2015-01-10  | 2
1  | 2015-02-11  | 3
1  | 2016-03-11  | 5
1  | 2017-05-07  | 3
1  | 2014-01-01  | 1
2  | 2014-01-10  | 7
2  | 2015-03-03  | 9
2  | 2016-08-08  | 8
2  | 2017-09-09  | 5

实际结果

===============================
id | 2014 | 2015 | 2016 | 2017
===============================
1  | 1    | 5    | 5    |3
2  | 7    | 9    | 8    |5

0 个答案:

没有答案