Solved

Sum NULL values SQL 2008

Posted on 2011-09-09
6
268 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

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 …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now