SQL - Returning all rows even if count is zero for item

前端 未结 3 1524
太阳男子
太阳男子 2020-12-20 22:06

I am performing a count based on a date range. Currently the query does return the correct result but I require additional information. In it\'s current form, the query show

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-20 22:25

    I think will need to count CCount.BaselineID and use a left join If you count on Tree.Type you would not get a zero on rows with no match
    And you do know that date range will return zero

    SELECT Tree.Type as 'Requirement Type'
         , COUNT(CCount.BaselineID) as 'Number in Creation Range' 
    FROM [Tree] 
    INNER JOIN @ReqType As RT 
       on RT.Type = Tree.Type
    INNER JOIN [Project_INFO]  
       ON [Project_INFO].[ProjectID] = [Tree].[Project_ID] 
    OUTER JOIN @CreationCount AS CCount 
       ON CCount.BaselineID=Tree.Baseline_ID 
    WHERE [Project_INFO].[Name] = 'Address Book' 
      AND CCount.Name = 'Current Baseline' 
      AND [Tree].[creationDate] >= ('2010-01-01') 
      and [Tree].[creationDate]  < ('2020-01-01') 
    GROUP BY tree.Type
    

提交回复
热议问题