如何将这两个查询合并为单个查询
1。 select sum(amount) as received from voucher where amount>0
2。 select sum(amount) as repaid from voucher where amount<0
答案 0 :(得分:3)
您可以使用条件聚合
select sum( case when amount > 0 then amount else 0 end ) as received,
sum( case when amount < 0 then amount else 0 end ) as repaid
FROM t
您可以在Postgres 9.4 +
中使用FILTER
select sum( amount ) filter (where amount > 0 ) as received,
sum( amount ) filter (where amount < 0 ) as repaid
FROM t
答案 1 :(得分:0)
试试这个。,
select case when amount>0 then sum(amount) end as 'received',
case when amount<0 then sum(amount) end as 'repaid' from voucher