Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Returning a column per date in a range Expand / Collapse
Author
Message
Posted Tuesday, January 10, 2012 9:55 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, October 8, 2013 5:42 AM
Points: 12, Visits: 83
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
Post #1233365
Posted Tuesday, January 10, 2012 10:05 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 3:52 AM
Points: 654, Visits: 508
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
Post #1233377
Posted Tuesday, January 10, 2012 10:24 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 1:52 PM
Points: 6,840, Visits: 13,354
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
Post #1233388
Posted Tuesday, January 10, 2012 10:49 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, October 17, 2014 8:13 AM
Points: 9,926, Visits: 11,188
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
Post #1233400
Posted Wednesday, January 11, 2012 6:51 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 3, 2013 8:24 AM
Points: 1,240, Visits: 5,421
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
Post #1233942
Posted Wednesday, January 11, 2012 7:03 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 3:52 AM
Points: 654, Visits: 508
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
Post #1233950
Posted Wednesday, January 11, 2012 7:08 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, October 17, 2014 8:13 AM
Points: 9,926, Visits: 11,188
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 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

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
Post #1233954
Posted Wednesday, January 11, 2012 7:46 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 3, 2013 8:24 AM
Points: 1,240, Visits: 5,421
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 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

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
Post #1233988
Posted Wednesday, January 11, 2012 8:14 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, October 17, 2014 8:13 AM
Points: 9,926, Visits: 11,188
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
Post #1234013
Posted Wednesday, January 11, 2012 8:51 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 3, 2013 8:24 AM
Points: 1,240, Visits: 5,421
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
Post #1234059
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse