Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sum NULL values SQL 2008

Posted on 2011-09-09
6
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 29

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 29

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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

719 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