如何在计数查询的结果中包含0

时间:2016-01-28 23:07:25

标签: sql sql-server join count aggregate-functions

我有4个表存储4种请求:时间关闭(TO),会议(MT),PO和旅行(TR)。

我想计算过去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,那么我只有结果:

enter image description here

如何在结果中包含0值,所以我会有类似的东西(如果会议MT计数= 0):

  

requestType --- Count

     

TO ------------- 30

     

MT ------------- 0

     

PO ------------- 19

     

TR ------------- 9

2 个答案:

答案 0 :(得分:1)

您必须从查询中删除GROUP BY,不需要它! 您的查询应该只是:

SELECT 'TO' AS [requestType], COUNT ([requestType]) As 'Count' 
  FROM [Timeoff] 
  WHERE [requestType] = 'TO' AND CAST([timeoffCreaime] ATE) >= CASt(DAY,-30,Gte()) ATE)
  UNION ALL
  SELECT 'Meeting' AS [requestType], COUNT ([requestType]) As 'Count' 
  FROM [Meeting] 
  WHERE [requestType] = 'Meeting' AND CAST([meetingCreaime] ATE) >= CASt(DAY,-30,Gte()) ATE)
  UNION ALL
  SELECT 'PO' AS [requestType], COUNT ([requestType]) As 'Count' 
  FROM [PO] 
  WHERE [requestType] = 'PO' AND CAST([poCreaime] ATE) >= CASt(DAY,-30,Gte()) ATE)
  UNION ALL
  SELECT 'TR' AS [requestType], COUNT ([requestType]) As 'Count' 
  FROM [Travel] 
  WHERE [requestType] = 'TR' AND CAST([travelCreaime] ATE) >= CASt(DAY,-30,Gte()) ATE)

答案 1 :(得分:0)

您可以对每个查询执行外连接,例如(select 1)

SELECT [requestType], COUNT ([requestType]) As 'Count' 
  FROM (select 1 as foo) bar
  LEFT JOIN [CTEite].[dbo].[Timeoff] ON 1=1
  WHERE [requestType] = 'TO' AND CAST([timeoffCreaime] ATE) >= CASt(DAY,-

或UNION和sum

WITH foo AS(
SELECT 'TO' as [requestType], 0 as [Count]
UNION ALL
SELECT 'MT', 0
UNION ALL
....
)
SELECT [requestType], sum([Count])
FROM
(SELECT [requestType], COUNT ([requestType]) As 'Count' 
  FROM [CTEite].[dbo].[Timeoff] 
  WHERE [requestType] = 'TO' AND CAST([timeoffCreaime] ATE) >= CASt(DAY,-30,Gte()) ATE) GROUP BY [requestType]
  UNION ALL
  SELECT [requestType], COUNT ([requestType]) As 'Count' 
  FROM [CTEite].[dbo].[Meeting] 
  WHERE [requestType] = 'Meeting' AND CAST([meetingCreaime] ATE) >= CASt(DAY,-30,Gte()) ATE) GROUP BY [requestType]
  UNION ALL
  SELECT [requestType], COUNT ([requestType]) As 'Count' 
  FROM [CTEite].[dbo].[PO] 
  WHERE [requestType] = 'PO' AND CAST([poCreaime] ATE) >= CASt(DAY,-30,Gte()) ATE) GROUP BY [requestType]
  UNION ALL
  SELECT [requestType], COUNT ([requestType]) As 'Count' 
  FROM [CTEite].[dbo].[Travel] 
  WHERE [requestType] = 'TR' AND CAST([travelCreaime] ATE) >= CASt(DAY,-30,Gte()) ATE) GROUP BY [requestType]
 UNION SELECT * FROM foo)
GROUP BY [requestType]