2012-10-05 11 views
5

Sto eseguendo un conteggio in base a un intervallo di date. Attualmente la query restituisce il risultato corretto ma ho bisogno di ulteriori informazioni. Nella sua forma attuale, la query mostra l'elemento con il conteggio corretto. Tuttavia ho bisogno di mostrare tutti gli elementi, anche se il loro conteggio è zero per l'intervallo di date specificato.SQL - Restituzione di tutte le righe anche se il conteggio è zero per la voce

Ecco il codice 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 

Quando eseguo questa query ottengo il seguente risultato:

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

Questo risultato è corretto però ho bisogno di tutti i tipi di requisiti per essere lista , anche se non ci sono requisiti nell'intervallo di creazione, ovvero

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

Ho provato a utilizzare vari join, IFNULL e ISNULL, ma non ho niente da lavorare.

Se qualcuno potesse indicarmi la giusta direzione lo apprezzerei.

+0

uso "outer join". – Ben

risposta

2

Modificare la seconda 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

Grazie Alexander, questa modifica restituisce il set di risultati che sto cercando, molto apprezzato. – user1454112

+0

Buona fortuna @ user1454112 –

0

Credo che sarà necessario contare CCount.BaselineID e utilizzare un LEFT JOIN Se si conta su Tree.Type non otterreste uno zero su file con alcuna corrispondenza
e tu sai che l'intervallo di date tornerà a 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

Grazie per avere dato un'occhiata a questo Blam – user1454112

+0

Hai provato? Questo userà e indicizzerà su [Tree]. [CreationDate]. – Paparazzi

1

in generale, per ottenere i record con i conteggi di 0, è necessario un join esterno di qualche tipo in modo da contare le righe che non hanno alcuna corrispondenza. Puoi anche usare un cross-join di tutte le opzioni per le quali vuoi contare. In alternativa, implemento spesso questo tipo di conteggio utilizzando una sottoquery correlata. Ecco un paio di esempi generali:

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

Un'altra possibilità, se hai una query di lavoro, è quello di incidere insieme qualcosa di simile:

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

Grazie per la risposta, Tim, mi rallegro che tu abbia dedicato del tempo a guardare questo – user1454112

+0

Sicuro. Speriamo che tutte le risposte spieghino in qualche modo perché non stavi ottenendo zero e come ottenerle. –

Problemi correlati