我有一個訂單表,我想要顯示一個報告,顯示從單個表中拒絕的月份,總訂單和總訂單。在單個查詢中顯示2個計數結果
表有dtcomplete,rtpID和supplierReject,我想用,這幾乎讓我有,但應該只有1拒絕顯示爲1月,我想子查詢中只檢查分組月
select datename(month, dtComplete) as Month, count(rtpID) as TotalOrders,
(select count(*) from RTPMaindetails where SupplierRejected = 1 and datename(month, dtComplete) = datename(month, RTPMaindetails.dtComplete) group by datepart(month,dtcomplete)) as Rejects
from RTPMaindetails
where datepart(year,dtComplete) = 2017
group by datepart(month,dtcomplete),datename(month, dtComplete)
order by datepart(month,dtcomplete)
表演:
Month TotalOrders Rejects
January 515 1
February 308 1
March 156 1
應該顯示
Month TotalOrders Rejects
January 515 1
February 308 0
March 156 0
哪些DBMS您使用的? Postgres的?甲骨文? –