SQL - 即使项目的count为零,也返回所有行

时间:2012-10-05 13:56:32

标签: sql sql-server tsql

我正在根据日期范围执行计数。目前查询确实返回了正确的结果,但我需要其他信息。在它的当前表单中,查询显示具有正确计数的项目。但是我需要显示所有项目,即使它们在指定的日期范围内的计数为零。

这是SQL代码:

INSERT INTO @CreationCount (BaselineID, Name)

SELECT distinct [BaselineID],[Name] 
FROM [Baseline_INFO] 

DECLARE @ReqType TABLE (Type nvarchar(128))
INSERT INTO @ReqType (Type)
SELECT DISTINCT Tree.Type as 'Requirement Type'
FROM [TREE]
INNER JOIN [Project_INFO]  ON [Project_INFO].[ProjectID]=[Tree].[Project_ID] 
INNER JOIN [Baseline_INFO] ON [Baseline_INFO].[BaselineID]=[Tree].[Baseline_ID]
WHERE [Project_INFO].[Name] = 'Address Book' AND [Baseline_INFO].[Name] = 'Current
Baseline' 
Group By Tree.Type

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

当我执行此查询时,我得到以下结果:

https://dl.dropbox.com/u/17234826/SQLresult.png

这个结果是正确的,但是我需要列出所有需求类型,即使在创建范围内没有要求,即

https://dl.dropbox.com/u/17234826/SQLresult1.png

我尝试使用各种连接,IFNULL和ISNULL,但我没有任何工作。

如果有人能指出我正确的方向,我会很感激。

3 个答案:

答案 0 :(得分:3)

修改第二个查询

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

答案 1 :(得分:2)

一般来说,要获取计数为0的记录,您需要某种外部联接,以便计算不匹配的行。您甚至可以使用所需的所有选项的交叉连接。或者,我经常使用相关子查询来实现这种类型的计数。以下是几个一般示例:

-- 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

另一种可能性,如果你有一个有效的查询,就是将这样的东西混在一起:

-- 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
)

答案 2 :(得分:1)

我认为需要计算CCount.BaselineID并使用左连接 如果你依靠Tree.Type,你就不会在没有匹配的行上得到零 并且你知道日期范围将返回零

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