Postgresql数据库的Sql查询
我在一个表中有4列Postgresql数据库的Sql查询,sql,postgresql,Sql,Postgresql,我在一个表中有4列 A <date> B <boolean> C <boolean> D <numeric> 期望结果 A E F 11/1/2014 90 20 12/1/2014 130 0 13/1/2014 75 14/1/2014 18 10 如果您使用的是Postgres 9.4,则可以通过使用过滤聚合使其更具可读性 select a, sum(d) filter
A <date>
B <boolean>
C <boolean>
D <numeric>
期望结果
A E F
11/1/2014 90 20
12/1/2014 130 0
13/1/2014 75
14/1/2014 18 10
如果您使用的是Postgres 9.4,则可以通过使用过滤聚合使其更具可读性
select a,
sum(d) filter (b) as e,
sum(d) filter (c) as f
group by a;
你能至少展示一下你到目前为止所做的尝试吗?请使用术语列,而不是字段。如果同一日期的每一行在B和C上都有不同的值怎么办?
select a,
sum(case when b then d end) as e,
sum(case when c then d end) as f
group by a;
select a,
sum(d) filter (b) as e,
sum(d) filter (c) as f
group by a;