2010-05-10 6 views
18

ho una stored procedure come segue:Modifica di un SUM ritornato NULL a zero

CREATE PROC [dbo].[Incidents] 
(@SiteName varchar(200)) 

AS 

SELECT 

( 
    SELECT SUM(i.Logged) 
    FROM tbl_Sites s 
    INNER JOIN tbl_Incidents i 
    ON s.Location = i.Location 
    WHERE s.Sites = @SiteName AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0) 
    GROUP BY s.Sites 
) AS LoggedIncidents 

'tbl_Sites contains a list of reported on sites. 
'tbl_Incidents containts a generated list of total incidents by site/date (monthly) 
'If a site doesnt have any incidents that month it wont be listed. 

Il problema che sto avendo è che un sito doesnt ha nessun Incidenti di questo mese e come tale ho un valore NULL restituito per quel sito quando eseguo questo sproc, ma ho bisogno di avere uno zero/0 restituito per essere utilizzato all'interno di un grafico in SSRS.

Ho provato l'utilizzo di coalesce e non è affatto inutile.

SELECT COALESCE(SUM(c.Logged,0)) 
    SELECT SUM(ISNULL(c.Logged,0)) 

C'è un modo per ottenere questo formattato correttamente?

Cheers,

Lee

risposta

35

Mettila fuori:

SELECT COALESCE(

( 
    SELECT SUM(i.Logged) 
    FROM tbl_Sites s 
    INNER JOIN tbl_Incidents i 
    ON s.Location = i.Location 
    WHERE s.Sites = @SiteName AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0) 
    GROUP BY s.Sites 
), 0) AS LoggedIncidents 

Se si restituisce più righe, modificare INNER JOIN per LEFT JOIN

SELECT COALESCE(SUM(i.Logged),0) 
FROM tbl_Sites s 
LEFT JOIN tbl_Incidents i 
ON s.Location = i.Location 
WHERE s.Sites = @SiteName AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0) 
GROUP BY s.Sites 

A modo, non mettere alcuna funzione o esprimere lo ione all'interno delle funzioni aggregate se non è garantito, ad es. non mettere ISNULL, COALESCE all'interno di SUM, usando la funzione/espressione all'interno aggregazione paralizza le prestazioni, la query sarà eseguita con scansione di tabella

+0

mi sono confuso tra molti anwsers che ho trovato per il rilascio stavo affrontando, ma alla fine "COALESCE (SUM (i.Logged) , 0) "ha fatto il trucco, e in un modo molto elegante e semplice. Grazie!! – TheCuBeMan

18

Dovrete usare ISNULL come questo -

ISNULL(SUM(c.Logged), 0)  

Or , come ha detto Michael, puoi usare un Join esterno sinistro.

1

Si potrebbe avvolgere la SELEZIONA in un altro SELEZIONA in questo modo:

CREATE PROC [dbo].[Incidents] 
(@SiteName varchar(200)) 

AS 

SELECT COALESCE(TotalIncidents ,0) 
FROM (
    SELECT 
    ( 
    SELECT SUM(i.Logged) as TotalIncidents 
    FROM tbl_Sites s 
    INNER JOIN tbl_Incidents i 
    ON s.Location = i.Location 
    WHERE s.Sites = @SiteName AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0) 
    GROUP BY s.Sites 
) AS LoggedIncidents 
) 
2

Il metodo più semplice e più leggibile, modo che ho trovato per farlo è attraverso:

CREATE PROC [dbo].[Incidents] 
(@SiteName varchar(200)) 

AS 

    SELECT SUM(COALESCE(i.Logged, 0)) AS LoggedIncidents 
    FROM tbl_Sites s 
    INNER JOIN tbl_Incidents i 
    ON s.Location = i.Location 
    WHERE s.Sites = @SiteName 
      AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0) 
    GROUP BY s.Sites 
+1

Se non vengono trovate righe in tbl_Sites, SUM non viene valutata e restituisce NULL. –

1

appena incontrato questo problema, la soluzione di Kirtan ha funzionato bene per me, ma la sintassi era un po 'fuori. Mi è piaciuto così:

ISNULL(SUM(c.Logged), 0) 

Post mi ha aiutato a risolvere il mio problema però grazie a tutti.

+0

ma ISNULL non trovato in Oracle – Gank

6

Ho incontrato questo problema in Oracle. Oracle non ha una funzione ISNULL(). Tuttavia, possiamo usare la funzione NVL() per ottenere lo stesso risultato:

NVL(SUM(c.Logged), 0) 
Problemi correlati