?
Solved

Return 0 for hours with no sales

Posted on 2009-07-07
10
Medium Priority
?
277 Views
Last Modified: 2012-05-07
I've got the following query:


SELECT DATEPART(hour, TimeStamp) AS Hour
, SUM(cast(Amount as money)) AS Sales
FROM billingTransactions
WHERE DATEADD(day, 0, DATEDIFF(day, 0, TimeStamp)) = '07/06/2009'
GROUP BY DATEPART(hour, TimeStamp)
ORDER BY hour

I want it to return a 0 for hours where there are no sales.....  This works well, but only returns those hours with actual sales.
0
Comment
Question by:pazzaTim
[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
  • 3
  • 3
  • 2
10 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24796809
;with cte (i) as (
Select 1 as i
union all
select i+1 as i
from cte )

SELECT i AS Hour
, ISNULL( SUM(cast(Amount as money)),0) AS Sales
FROM billingTransactions
LEFT Join CTE c ON c.i = DATEPART(hour, TimeStamp)
WHERE DATEADD(day, 0, DATEDIFF(day, 0, TimeStamp)) = '07/06/2009'
GROUP BY DATEPART(hour, TimeStamp)
order by hour
OPTION (MAXRECURSION 24)
0
 

Author Comment

by:pazzaTim
ID: 24796859
Not working... First it said that cte.i not in group by, when I added it, we got max recursions...
0
 

Author Comment

by:pazzaTim
ID: 24796898
The statement terminated. The maximum recursion 24 has been exhausted before statement completion.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1000 total points
ID: 24796953
instead of cte you can populate a temptable with 24 hours and you juts need to join that table

SELECT i AS Hour
, ISNULL( SUM(cast(Amount as money)),0) AS Sales
FROM billingTransactions
LEFT Join @tempTable  c ON c.i = DATEPART(hour, TimeStamp)
WHERE DATEADD(day, 0, DATEDIFF(day, 0, TimeStamp)) = '07/06/2009'
GROUP BY DATEPART(hour, TimeStamp)
order by hour
OPTION (MAXRECURSION 24)
0
 

Author Comment

by:pazzaTim
ID: 24797783
SELECT CTE.HR, ISNULL(SS.TOTAL,0) AS TOTAL
FROM (
SELECT DISTINCT DATEPART(hour, TimeStamp) AS HR
FROM billingTransactions
) CTE
LEFT OUTER JOIN (
SELECT DATEPART(hour, TimeStamp) AS HR, SUM(CAST(Amount AS money)) AS TOTAL
FROM billingTransactions
WHERE DATEADD(day, 0, DATEDIFF(day, 0, TimeStamp)) = '07/06/2009'
GROUP BY DATEPART(hour, TimeStamp)
) SS
ON CTE.HR = SS.HR
order by hr
0
 
LVL 15

Assisted Solution

by:rob_farley
rob_farley earned 1000 total points
ID: 24800925
If you were to archive off most of your records in billingTransactions, you'll find this no longer produces all your hours.

So why not go with:

WITH AllHours AS (
SELECT 0 AS Hour
UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10
UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20
UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23)
SELECT H.Hour, SS.Total
FROM AllHours H
  LEFT OUTER JOIN (
SELECT DATEPART(hour, TimeStamp) AS HR, SUM(Amount) AS Total
FROM billingTransactions
WHERE TimeStamp >= '20090706' AND TimeStamp < '20090707'
GROUP BY DATEPART(hour, TimeStamp)
) SS
ON H.Hour = SS.HR
ORDER BY H.Hour;

You'll also notice that I have changed the WHERE clause, so that I'm not fiddling with the TimeStamp column. This means that a non-clustered index like:

create index ixTimeStampAmont on billingTransactions(TimeStamp) include (Amount);

...will make this query much faster.

Also, the ORDER BY is now specifying a table alias, to make sure there's no ambiguity.

Hope this helps complete the picture...

Rob
0
 
LVL 15

Expert Comment

by:rob_farley
ID: 24800936
pazzaTim should assign some points here...
0
 
LVL 15

Expert Comment

by:rob_farley
ID: 24833321
I think if pazzaTim found that the answer he wrote was the best, then it's fine to Accept that one as the answer, but as the Asker, he should've given the points to those that helped.

So... Accept with 50, and give 450 away to the other experts who contributed.

Rob
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

771 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