Solved

Sum NULL values SQL 2008

Posted on 2011-09-09
6
275 Views
Last Modified: 2012-05-12
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!
0
Comment
Question by:rmm2001
  • 2
  • 2
  • 2
6 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 36513635
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

0
 
LVL 7

Author Comment

by:rmm2001
ID: 36513698
That does the same as the one I posted (without the COUNT) in it. It skips the NULL lines
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 36514383
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
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 22

Expert Comment

by:Thomasian
ID: 36515358
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

0
 
LVL 22

Accepted Solution

by:
Thomasian earned 500 total points
ID: 36515379
Upon rereading your question, it seem that you already have a table with a complete list of dates (DateTable). If so, you won't need to create a derived table.

If you have a table with a distinct list of colors, you can also replace the ColorList with it.
;WITH ColorList As (
	SELECT DISTINCT color FROM dbo.Color
)
SELECT CL.Color, D.ShortDate, COUNT(C.ShortDate) As [Total]
FROM dbo.DateTable D CROSS JOIN ColorList CL
     LEFT JOIN dbo.Color C ON D.ShortDate = C.Created AND CL.color = C.color
GROUP BY CL.Color, D.ShortDate
ORDER BY CL.Color, D.ShortDate

Open in new window

0
 
LVL 7

Author Closing Comment

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

Thank you so much!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question