我有4個表存儲4種請求:延時(TO),會議(MT),訂單和旅行(TR)。如何在計數查詢結果中包含0
我要計算每個表中每種類型的請求總數在過去30天:
SELECT [requestType], COUNT ([requestType]) As 'Count'
FROM [Timeoff]
WHERE [requestType] = 'TO' AND CAST([timeoffCreatedTime] AS DATE) >= CAST(DateAdd(DAY,-30,Getdate()) AS DATE) GROUP BY [requestType]
UNION ALL
SELECT [requestType], COUNT ([requestType]) As 'Count'
FROM [Meeting]
WHERE [requestType] = 'MT' AND CAST([meetingCreatedTime] AS DATE) >= CAST(DateAdd(DAY,-30,Getdate()) AS DATE) GROUP BY [requestType]
UNION ALL
SELECT [requestType], COUNT ([requestType]) As 'Count'
FROM [PO]
WHERE [requestType] = 'PO' AND CAST([poCreatedTime] AS DATE) >= CAST(DateAdd(DAY,-30,Getdate()) AS DATE) GROUP BY [requestType]
UNION ALL
SELECT [requestType], COUNT ([requestType]) As 'Count'
FROM [Travel]
WHERE [requestType] = 'TR' AND CAST([travelCreatedTime] AS DATE) >= CAST(DateAdd(DAY,-30,Getdate()) AS DATE) GROUP BY [requestType]
問題是,如果計數爲0,那麼它不會在結果顯示在所有,例如,如果會議請求的數量= 0,那麼我唯一有此作爲的結果:
怎樣包括在結果中的0值,所以我有類似的東西(如會議MT數= 0):
的RequestType ---計數
TO ------------- 30
MT ------------- 0
PO ------------- 19
TR ------------- 9
你爲什麼要寫Group By子句? 我認爲這不需要,因爲我的書面解決方案 –