Link to home
Start Free TrialLog in
Avatar of rmm2001
rmm2001Flag for United States of America

asked on

Sum NULL values SQL 2008

Hi All -

I think this can be done fairly easily I'm just not sure how. Here's what it is

I'm trying to make a running total from a list of values I have in a table. But I want the ENTIRE list - not just sums where there are values (I want to make NULLs be 0's instead).

So here's what the table looks like:

Red 4/1/2010      1
Red 4/1/2010        1
Red 4/2/2010      7
Red 4/6/2010      10
Blue ...
Green ...

And here's the sort of output I want. (Notice the 0's are still there even though there's no raw data for them)
Red 4/1/2010      2
Red 4/2/2010      1
Red 4/3/2010      0
Red 4/4/2010      0
Red 4/5/2010      0
Red 4/6/2010      10
Blue...
Green...

This is missing something.

SELECT ShortDate,
         Color,
       ISNULL(COUNT(9), 0)
FROM dbo.Color b
LEFT JOIN dbo.DateTable dt on dt.ShortDate = b.Created
GROUP BY Color, ShortDate
ORDER BY ShortDate, Color

Any ideas on how to make my running totals appear? I'm on 2008 R2.

Thanks!
Avatar of sammySeltzer
sammySeltzer
Flag of United States of America image

It is more like:

SELECT ShortDate,
         Color,
       COUNT(ISNULL(fieldname, 0)) as TotalCount
FROM dbo.Color b
LEFT JOIN dbo.DateTable dt on dt.ShortDate = b.Created
GROUP BY Color, ShortDate
ORDER BY ShortDate, Color

Avatar of rmm2001

ASKER

That does the same as the one I posted (without the COUNT) in it. It skips the NULL lines
sorry my fault  - silly me

SELECT ShortDate,
         Color,
       ISNULL(COUNT(fieldname), 0) as TotalCount
FROM dbo.Color b
LEFT JOIN dbo.DateTable dt on dt.ShortDate = b.Created
GROUP BY Color, ShortDate
ORDER BY ShortDate, Color

Open in new window



Come to think of it, your code should have worked.

Just alias it

SELECT ShortDate,
         Color,
       ISNULL(COUNT(9), 0) as TOTAL
FROM dbo.Color b
LEFT JOIN dbo.DateTable dt on dt.ShortDate = b.Created
GROUP BY Color, ShortDate
ORDER BY ShortDate, Color

Open in new window


Just look under TotalCount for mine or Total for yours.

You can also use coalesce

in place of isnull. You get same result
The problem is the "missing" date does not exist on the table so it will never be returned.

There are a few ways to "fill" up missing dates. One way is to create a derived table (which is done through a recursive query) which contains a list of dates and outer join it to your table.
DECLARE @t table (color varchar(100), ShortDate date)

INSERT @t
SELECT 'Red', '4/1/2010'
UNION ALL SELECT 'Red', '4/1/2010'
UNION ALL SELECT 'Red', '4/2/2010'
UNION ALL SELECT 'Red', '4/6/2010'
UNION ALL SELECT 'Blue', '4/1/2010'
UNION ALL SELECT 'Green', '4/6/2010'

;WITH Dates AS (
	SELECT MIN(ShortDate) ShortDate, MAX(ShortDate) LastDate
	FROM @t
	
	UNION ALL
	
	SELECT DATEADD(DAY,1,ShortDate), LastDate
	FROM Dates
	WHERE LastDate>ShortDate
), Colors As (
	SELECT DISTINCT color FROM @t
)
SELECT C.Color, D.ShortDate, COUNT(T.ShortDate) As [Total]
FROM Dates D  CROSS JOIN Colors C
     LEFT JOIN @t T ON D.ShortDate=T.ShortDate AND C.color=T.color
GROUP BY C.color, D.ShortDate 
ORDER BY C.color, D.ShortDate
OPTION (MAXRECURSION 32767)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Thomasian
Thomasian
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmm2001

ASKER

That's awesome! It's the CROSS JOIN that was throwing me for a loop when I was attempting.

Thank you so much!