简体   繁体   English

使用 1 个查询获取 2 个查询的结果

[英]Get result of 2 queries with 1 query

I have 2 following queries in the same table transactions .我在同一个表transactions有 2 个以下查询。

transactions table has following 2 columns which is relevant here, registrationId , total and totalPaid . transactions表有以下 2 列,这里是相关的, registrationIdtotaltotalPaid

To get all the paid transactions I run following query为了获得我运行以下查询的所有付费交易

Select SUM(transactions.totalPaid) as netPaid from transactions where deleted is null group by registrationId

To get all the unpaid transactions I run the following query为了获得所有未支付的交易,我运行以下查询

Select SUM(transactions.total - transactions.totalPaid) as unPaid from transactions where ((totalPaid < total) OR (total < 0)) and deleted is null group by registrationId

How can I combine the query and get both result in one go?如何组合查询并一次性获得两个结果?

Thank you谢谢

You can use case statement to the the unPaid column.您可以对unPaid列使用case语句。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM