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

前端 未结 3 1476
太阳男子
太阳男子 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:22

    Generally speaking, to get records with counts of 0, you need an outer join of some sort so you count the rows that have no match. You can even use a cross-join of all the options you want counts for. Alternatively, I often implement this type of count by using a correlated subquery. Here are a couple of general examples:

    -- Get count using left join
    select c.customer_id,
        count(o.order_id) as num
    from customers c
        left join orders o on c.customer_id = o.customer_id
    group by c.customer_id
    
    -- Get count using correlated subquery
    select c.customer_id,
        (select count(*) from orders where customer_id = c.customer_id) as Num
    from customers c
    

    Another possibility, if you've got a working query, is to hack together something like this:

    -- Create a cte of the original query that we will use multiple times
    ;WITH cte as (
        SELECT Tree.Type as 'Requirement Type'
            , COUNT(Tree.Type) 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] 
            INNER 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
    )
    -- Grab the counts of records with matches
    select *
    from cte
    -- Grab the zero counts (records not in the original query)
    union all
    select Tree.Type, 0
    from [Tree]
    where Tree.Type not in (
        select Tree.Type
        from cte
    )
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-20 22:33

    Modify the second query

    SELECT Tree.Type as 'Requirement Type',
           COUNT(CASE WHEN [Tree].[creationDate] >= ('2010-01-01') and [Tree].[creationDate] < ('2020-01-01') THEN Tree.Type END) 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] 
    INNER JOIN @CreationCount AS CCount ON CCount.BaselineID=Tree.Baseline_ID 
    WHERE [Project_INFO].[Name] = 'Address Book' AND CCount.Name = 'Current Baseline' 
    GROUP BY tree.Type
    
    0 讨论(0)
提交回复
热议问题