Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在单个查询中组合这两个查询_Sql_Postgresql - Fatal编程技术网

Sql 如何在单个查询中组合这两个查询

Sql 如何在单个查询中组合这两个查询,sql,postgresql,Sql,Postgresql,如何将这两个查询组合成单个查询 1.选择从金额>0的凭证收到的金额(金额) 2.选择从凭证中偿还的金额您可以使用条件聚合 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 您可以使用条件聚合 select sum( case when amo

如何将这两个查询组合成单个查询

1.
选择从金额>0的凭证收到的金额(金额)


2.
选择从凭证中偿还的金额您可以使用条件聚合

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

您可以使用条件聚合

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
试试这个

select case when amount>0 then sum(amount) end as 'received', 
case when amount<0 then sum(amount) end as 'repaid' from voucher
选择金额大于0时的情况,然后总和(金额)以“已收到”结束,
当数量时,请尝试此操作

select case when amount>0 then sum(amount) end as 'received', 
case when amount<0 then sum(amount) end as 'repaid' from voucher
选择金额大于0时的情况,然后总和(金额)以“已收到”结束,

当amount@JitinSasidharan:不客气。请读:@jitinasidharan:不客气。请阅读: