Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Returning 0's in a SUM (SQL Spackle) Rate Topic Display Mode Topic Options
Author
 Message
 Posted Sunday, June 12, 2011 10:43 PM
 SSCrazy Group: General Forum Members Last Login: Monday, December 09, 2013 11:03 AM Points: 2,608, Visits: 648
 Comments posted to this topic are about the item Returning 0's in a SUM (SQL Spackle)
Post #1124040
 Posted Monday, June 13, 2011 1:45 AM
 Forum Newbie Group: General Forum Members Last Login: Thursday, August 15, 2013 3:14 AM Points: 5, Visits: 94
 Looks too much complicated for the goal, isn't it ?
Post #1124077
 Posted Monday, June 13, 2011 2:13 AM
 Valued Member Group: General Forum Members Last Login: Wednesday, September 11, 2013 9:36 AM Points: 58, Visits: 201
 There may be other ways but apart from using a dedicated number table rather than spt_values, this is exactly the way I do it.I am intrigued by the comment that this looks too complicated. I would like to see a simpler example.In short, create a calendar table that holds the date / time / base value range and then embellish it with the actual data. How can this be achieved any simpler? Please share.A good article in my opinion.Dave David BridgeDavid Bridge Technology Limitedwww.davidbridgetechnology.com
Post #1124085
 Posted Monday, June 13, 2011 2:38 AM
 SSC Rookie Group: General Forum Members Last Login: Monday, September 02, 2013 8:04 AM Points: 36, Visits: 193
 HiCan use cross-join with hour tableThis is easier solution by more costly on the execution plan
Post #1124098
 Posted Monday, June 13, 2011 5:18 AM
 SSC Journeyman Group: General Forum Members Last Login: Sunday, December 08, 2013 11:40 PM Points: 77, Visits: 473
 This should be a simpler solution.`select N2.n as dan, N.n - 1 as Ura, isnull(sum(SaleQTY), 0) as Dfrom dbo.GetNums(24) as N cross join dbo.GetNums(31) as N2 left join (select datepart(day, SaleTime) as Dan, datepart(hh, SaleTime) as Ura, sum(SaleQTY) as SaleQTY from dbo.#SalesTestData group by datepart(day, SaleTime), datepart(hour, SaleTime))as D on D.Dan = N2.n and D.Ura = (N.n - 1)group by N2.n, N.norder by 1, 2`Uncle Goole will tell you more about function GetNums ( = Virtual Auxiliary Table of Numbers).
Post #1124165
 Posted Monday, June 13, 2011 6:29 AM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 11:46 AM Points: 34,581, Visits: 28,767
 Matjaz Justin (6/13/2011)This should be a simpler solution.`select N2.n as dan, N.n - 1 as Ura, isnull(sum(SaleQTY), 0) as Dfrom dbo.GetNums(24) as N cross join dbo.GetNums(31) as N2 left join (select datepart(day, SaleTime) as Dan, datepart(hh, SaleTime) as Ura, sum(SaleQTY) as SaleQTY from dbo.#SalesTestData group by datepart(day, SaleTime), datepart(hour, SaleTime))as D on D.Dan = N2.n and D.Ura = (N.n - 1)group by N2.n, N.norder by 1, 2`Uncle Goole will tell you more about function GetNums ( = Virtual Auxiliary Table of Numbers).Although I'm sure that Uncle Google will tell us about the GetNums, it would be real handy if you'd simply provide the URL in the future since you're the one that brought up the function. --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #1124197
 Posted Monday, June 13, 2011 6:31 AM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 11:46 AM Points: 34,581, Visits: 28,767
 harry9katz (6/13/2011)HiCan use cross-join with hour tableThis is easier solution by more costly on the execution planCool! Got code? --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #1124201
 Posted Monday, June 13, 2011 6:41 AM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 11:46 AM Points: 34,581, Visits: 28,767
 Folks, the GetNums function that Matjaz reference is Itzek Ben-Gan's cascading Cross Join code wrapped in a function. If you decide to use that instead of some other method, make sure you use the code that has "TOP" in it for the very reasons that Itzek mentions and the fact that I've also confirmed it's a bit faster than his previous rendition. Itsek's article can be found at the following URL:http://www.sqlmag.com/article/sql-server/virtual-auxiliary-table-of-numbers --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #1124211
 Posted Monday, June 13, 2011 6:43 AM
 SSC Rookie Group: General Forum Members Last Login: Wednesday, June 26, 2013 12:05 PM Points: 35, Visits: 78
 Although the "GetNums" function would indeed make this simpler in-line, it simply moves the complexity to another routine. Combined, the two parts have nearly the same code. That having been said, standardizing common functions like getnums() is a good practice. The article is probably best viewed as a general approach to reporting on "sparse data". Instead of hours or dates, consider regions, possible questionnaire responses, etc.I've found that using in-memory tables and "building" results incrementally to be much clearer and less error-prone than highly complex joins, and with very acceptable performance (consider the use case...) I've seen many cases where the added developer time in debugging and maintaining far exceeds the additional performance time over the life of the application.
Post #1124215
 Posted Monday, June 13, 2011 6:44 AM
 Forum Newbie Group: General Forum Members Last Login: Thursday, August 15, 2013 3:14 AM Points: 5, Visits: 94
 No need for uncle Google in this case, spt_values is quite enough, unless you are querying some large data warehouse.However, I agree that GetNums is a cool trick to generate large order numbers.I was thinking of the following solution:I first add one computed column in the fact table (I assume that it is ok to do that) in order to keep the query SARGable. The new column will have the sales datetime rounded up to hour percision.ALTER TABLE #SalesTestDataADD SaleTimeHour AS CONVERT(DATETIME, CONVERT(VARCHAR,SaleTime,112)+' '+CAST(DATEPART(HOUR,SaleTime) AS VARCHAR(2))+':00:00.000',112) PERSISTEDThe report will then be formed by this single query:DECLARE @StartDate DATETIME, @EndDate DATETIMESET @StartDate='2011-01-01 00:00:00.000'SET @EndDate='2011-12-01 00:00:00.000'SELECT CAST(AllDates.ReportDayHour AS DATE) AS ReportDay, CAST(AllDates.ReportDayHour AS TIME) AS ReportHour, ISNULL(SUM(s.SaleQty),0) AS TotalSaleQtyFROM ( SELECT DATEADD(hh,h.number,DATEADD(dd,d.number,DATEADD(mm,m.number,@StartDate))) AS ReportDayHour FROM master..spt_values m CROSS JOIN master..spt_values d CROSS JOIN master..spt_values h WHERE m.type='p' AND m.number BETWEEN 0 AND DATEDIFF(mm,@StartDate,@EndDate) AND d.type='p' AND d.number BETWEEN 0 AND 30 AND h.type='p' AND h.number BETWEEN 0 AND 23) AS AllDatesLEFT JOIN #SalesTestData s ON s.SaleTimeHour=AllDates.ReportDayHourGROUP BY AllDates.ReportDayHourORDER BY AllDates.ReportDayHourThe parameters are the start and end moments of the reports, as any manager would want :)
Post #1124217

 Permissions