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
Solved

Sum NULL values SQL 2008

Posted on 2011-09-09
6
278 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
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.

 
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.

Question has a verified solution.

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

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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