遍历 Postgres 中的列
Looping across the column in Postgres
我table喜欢:
Name Stages Year Units
P1 Under Construction 2015 10
P2 Completed 2016 20
P3 Under Construction 2015 30
P4 Completed 2016 40
P5 Under Construction 2016 50
我的输出应该是这样的:
Stages 2015 2016
Under Construction 40 50
Completed 0 60
我试过这样的查询:
select sum(units) from table_1 where
stages = 'Under Construction' and year = 2016 ;
只使用条件聚合:
select stages,
sum(case when year = 2015 then units else 0 end) from units_2015,
sum(case when year = 2016 then units else 0 end) from units_2016
from t
group by stages;
我table喜欢:
Name Stages Year Units
P1 Under Construction 2015 10
P2 Completed 2016 20
P3 Under Construction 2015 30
P4 Completed 2016 40
P5 Under Construction 2016 50
我的输出应该是这样的:
Stages 2015 2016
Under Construction 40 50
Completed 0 60
我试过这样的查询:
select sum(units) from table_1 where
stages = 'Under Construction' and year = 2016 ;
只使用条件聚合:
select stages,
sum(case when year = 2015 then units else 0 end) from units_2015,
sum(case when year = 2016 then units else 0 end) from units_2016
from t
group by stages;