|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, January 11, 2013 3:50 AM
Points: 12,
Visits: 82
|
|
Guys, sorry for the late reply (been out of office). Have just checked out the code Paul posted and it works like a dream! I now just need to get my head fully around it so I can add a few bells and whistles.
Thanks so much for your replies, especially Paul.
I'll post my questions with some proper sample code next time!
Nick
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Today @ 1:28 AM
Points: 515,
Visits: 391
|
|
Nick -
I don't know if it matters, but Paul's solution doesn't quite do what you specified, as it doesn't return data for the last date in the report range when nobody was using a room. This is a frequent problem I have when I create reports for people - you can't easily report on data that isn't there! I started to look at an alternative approach, using a recursive CTE to generate a continuous list of dates that covered the report range (that worked a treat, btw), with the intent of then using that in a 'left-join' approach with the occupation data. But 'real work' got in the way, so I didn't get time to finish it. Anyway - good luck with it all.
Bob
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 12:04 PM
Points: 6,739,
Visits: 12,167
|
|
Bob Cullen-434885 (1/10/2012) Nick -
I don't know if it matters, but Paul's solution doesn't quite do what you specified, as it doesn't return data for the last date in the report range when nobody was using a room. This is a frequent problem I have when I create reports for people - you can't easily report on data that isn't there! I started to look at an alternative approach, using a recursive CTE to generate a continuous list of dates that covered the report range (that worked a treat, btw), with the intent of then using that in a 'left-join' approach with the occupation data. But 'real work' got in the way, so I didn't get time to finish it. Anyway - good luck with it all.
Bob
The issue you describe is different than the missing last date in Pauls code: gaps in data are covered by a dynamic calendar table in Pauls code. To include the last date, just change
SELECT TOP(DATEDIFF(DAY, R.CheckIn, R.CheckOut)) to SELECT TOP(DATEDIFF(DAY, R.CheckIn, R.CheckOut)+1)
Lutz A pessimist is an optimist with experience.
How to get fast answers to your question How to post performance related questions Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Yesterday @ 10:48 AM
Points: 10,990,
Visits: 10,578
|
|
LutzM (1/10/2012)
an the missing last date in Pauls code: gaps in data are covered by a dynamic calendar table in Pauls code. To include the last date, just change SELECT TOP(DATEDIFF(DAY, R.CheckIn, R.CheckOut)) to SELECT TOP(DATEDIFF(DAY, R.CheckIn, R.CheckOut)+1) Off-by-one errors, my speciality
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 02, 2013 1:20 PM
Points: 1,235,
Visits: 5,389
|
|
Bob Cullen-434885 (1/10/2012)
I started to look at an alternative approach, using a recursive CTE to generate a continuous list of dates that covered the report range
Jeff Moden has an article that shows how badly recursive CTEs can perform for certain types of tasks and it sounds like yours may fall into that category. Check out his article Hidden RBAR: Counting with Recursive CTE's
Drew
J. Drew Allen Business Intelligence Analyst Philadelphia, PA
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Today @ 1:28 AM
Points: 515,
Visits: 391
|
|
Hmmm. Duly checked out!! That's something of an eye-opener Won't be doing that again. Thanks for the pointer to a very interesting article.
Bob
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Yesterday @ 10:48 AM
Points: 10,990,
Visits: 10,578
|
|
drew.allen (1/11/2012)
Bob Cullen-434885 (1/10/2012)
I started to look at an alternative approach, using a recursive CTE to generate a continuous list of dates that covered the report rangeJeff Moden has an article that shows how badly recursive CTEs can perform for certain types of tasks and it sounds like yours may fall into that category. Check out his article Hidden RBAR: Counting with Recursive CTE's It's pretty tough to imagine a list of dates long enough to make any practical difference regarding performance here. Before anyone says it, yes I know that it often makes sense to code for the 10-million row case, but that would take us into the year 4749 here. A mere 4000 rows (150ms from Jeff's article graph) is more than enough to cover a ten-year period.
I congratulate Bob for finding my error and pursuing an alternative line of enquiry. My concern is that we shouldn't be too keen in promoting The One True Way to generate numbers such that we end up stifling innovation and exploration.
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 02, 2013 1:20 PM
Points: 1,235,
Visits: 5,389
|
|
SQL Kiwi (1/11/2012)
drew.allen (1/11/2012)
Bob Cullen-434885 (1/10/2012)
I started to look at an alternative approach, using a recursive CTE to generate a continuous list of dates that covered the report rangeJeff Moden has an article that shows how badly recursive CTEs can perform for certain types of tasks and it sounds like yours may fall into that category. Check out his article Hidden RBAR: Counting with Recursive CTE's It's pretty tough to imagine a list of dates long enough to make any practical difference regarding performance here. Before anyone says it, yes I know that it often makes sense to code for the 10-million row case, but that would take us into the year 4749 here. A mere 4000 rows (150ms from Jeff's article graph) is more than enough to cover a ten-year period. I congratulate Bob for finding my error and pursuing an alternative line of enquiry. My concern is that we shouldn't be too keen in promoting The One True Way to generate numbers such that we end up stifling innovation and exploration.
If you read the article, you'll see that even the mere 4000 row CTE has a significant performance impact. That's how badly recursive CTEs can perform.
Drew
J. Drew Allen Business Intelligence Analyst Philadelphia, PA
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Yesterday @ 10:48 AM
Points: 10,990,
Visits: 10,578
|
|
drew.allen (1/11/2012) If you read the article, you'll see that even the mere 4000 row CTE has a significant performance impact. That's how badly recursive CTEs can perform. You might have missed that I quoted the performance impact directly from the article "(150ms from Jeff's article graph)", which is one I have read many times (see the article's discussion thread for my comments). 150ms is not 'significant' in the current context, and it is 46ms on my laptop in any case:
SET STATISTICS TIME ON; WITH R AS (SELECT 1 AS N UNION ALL SELECT R.N + 1 FROM R WHERE R.N <= 4000) SELECT * INTO #bucket FROM R OPTION (MAXRECURSION 0) SET STATISTICS TIME OFF;
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 02, 2013 1:20 PM
Points: 1,235,
Visits: 5,389
|
|
SQL Kiwi (1/11/2012)
You might have missed that I quoted the performance impact directly from the article "(150ms from Jeff's article graph)", which is one I have read many times (see the article's discussion thread for my comments). 150ms is not 'significant' in the current context, and it is 46ms on my laptop in any case:
I disagree. 150ms is THIRTY times worse than the next worse algorithm. I think that's pretty significant. I can see no reason to recommend incorporating this method into new code when we know how badly it performs.
Granted, if this method was being used in production code you would have to consider the context to determine whether it was worth rewriting the code to improve performance, but that's not the case here.
Drew
J. Drew Allen Business Intelligence Analyst Philadelphia, PA
|
|
|
|