我需要針對3種不同的狀態值編寫彙總報告,每種狀態都有一個計數和一個金額列,結果顯示在一張表中。例如,輸出應該是這樣的:我想編寫一個彙總查詢並將結果呈現在一個表中
以產生每行代碼(在一個單獨的輸出)該查詢是:
select case when status_key = '2' then 'Paid' else '' end as 'Status'
, COUNT(BillNo) as [Count]
, SUM(amtpd) as [Amount Paid]
from billtable
where client = 101
and status_key = '2'
group by status_key
select case when status_key = '1' then 'Queued' else '' end as 'Status'
, COUNT(BillNo) as [Count]
, SUM(amtpd) as [Amount Paid]
from billtable
where client = 101
and status_key = '1'
group by status_key
select case when status_key = '4' then 'Hold' else '' end as 'Status'
, COUNT(BillNo) as [Count]
, SUM(amtpd) as [Amount Paid]
from billtable
where client = 101
and status_key = '4'
group by status_key
這產生三個結果一樣:
我使用SQL Server數據庫和SSMS發展查詢。
爲什麼不使用UNION? – TlmaK0
謝謝。我做了,當然它工作。 –