我在同一表transactions
中有2个以下查询。
transactions
表具有以下两列,在这里与之相关:registrationId
,total
和totalPaid
。
要获取所有付费交易,我会在查询后运行
Select SUM(transactions.totalPaid) as netPaid from transactions where deleted is null group by registrationId
要获取所有未付交易,我运行以下查询
Select SUM(transactions.total - transactions.totalPaid) as unPaid from transactions where ((totalPaid < total) OR (total < 0)) and deleted is null group by registrationId
如何合并查询并一次性获得两个结果?
谢谢
答案 0 :(得分:2)
您可以在case
列中使用unPaid
语句。
Select sum(transactions.totalPaid) as netPaid
, sum(case when (totalPaid < total) OR (total < 0) then transactions.total - transactions.totalPaid else 0 end) as unPaid
from transactions
where deleted is null
group by registrationId