2012-10-05 48 views
5

我正在執行基於日期範圍的計數。目前查詢確實返回了正確的結果,但我需要更多信息。在它的當前形式下,查詢顯示具有正確計數的項目。不過,我需要顯示所有項目,即使在指定的日期範圍內它們的計數爲零。SQL - 返回所有行,即使計數爲零,項目

下面是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,但我沒有任何工作。

如果有人能指出我在正確的方向,我會很感激。

+0

使用「外部聯接」。 – Ben

回答

2

修改第二查詢

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

感謝亞歷山大,這個修改返回了我正在尋找的結果集,非常感謝。 – user1454112

+0

祝你好運@ user1454112 –

0

我認爲將需要數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 
+0

謝謝你看看這個Blam – user1454112

+0

你試過了嗎?這將在[Tree]。[creationDate]上使用和索引。 – Paparazzi

1

一般來說,要獲得與記錄的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 
) 
+0

感謝Tim的迴應,感謝您花時間看這 – user1454112

+0

當然。希望所有的答案都能清楚地說明你爲什麼沒有達到零,以及如何去獲得它們。 –