|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, January 11, 2013 3:50 AM
Points: 12,
Visits: 82
|
|
Hi
I need to take a set of records for people with hotel check-in/check-out dates, and display a table that has a column for each date in the range, with a 1 if they need a room that night, or 0 if they don't.
e.g. PersonID,Name,Checkin,Checkout 1,Fred,2012-01-02,2012-01-05 2,Bob,2012-01-01,2012-01-04
would produce the result:
PersonID,Name,Jan 1 2012,Jan 2 2012,Jan 3 2012,Jan 4 2012,Jan 5 2012 1,Fred,0,1,1,1,0 2,Bob,1,1,1,0,0
(people need a room on the night they check in, but not on the night they check out).
I'm not sure where to start! It looks like something that pivots are meant to address, but don't fully understand them yet, a stored proc or a table-value function. I'm inclined to think the table-value function would give me the most flexibility as I could join PersonID to other tables to get more data about the people if needed.
Any suggestions/similar sample code would be much appreciated.
Nick
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 4:07 PM
Points: 1,943,
Visits: 8,227
|
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:37 AM
Points: 921,
Visits: 3,748
|
|
nick-1043370 (1/6/2012) Hi
I need to take a set of records for people with hotel check-in/check-out dates, and display a table that has a column for each date in the range, with a 1 if they need a room that night, or 0 if they don't.
e.g. PersonID,Name,Checkin,Checkout 1,Fred,2012-01-02,2012-01-05 2,Bob,2012-01-01,2012-01-04
would produce the result:
PersonID,Name,Jan 1 2012,Jan 2 2012,Jan 3 2012,Jan 4 2012,Jan 5 2012 1,Fred,0,1,1,1,0 2,Bob,1,1,1,0,0
(people need a room on the night they check in, but not on the night they check out).
I'm not sure where to start! It looks like something that pivots are meant to address, but don't fully understand them yet, a stored proc or a table-value function. I'm inclined to think the table-value function would give me the most flexibility as I could join PersonID to other tables to get more data about the people if needed.
Any suggestions/similar sample code would be much appreciated.
Nick
Can you provide some more detail about the date range, Nick? Will it always be a single month, starting at the 1st? = simple Random number of days, starting at random DOM = harder.
Cheers
Low-hanging fruit picker and defender of the moggies
For better assistance in answering your questions, please read this.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, January 11, 2013 3:50 AM
Points: 12,
Visits: 82
|
|
Dave - Fair point, but there's a bit of (dull) background as to why I'm looking down other paths...
As it happens we do it in SSRS at the moment, but to keep my post from confusing everyone I didn't mention that once I've got the data returned in column-per-date format I then want to do some calculations, such as room cost, total room cost for all people each night etc.
If I use an SSRS matrix/tablix then I really struggle to then do the calculations because there's no handle to grab on to for each date if the columns are generated at runtime. I would set fixed columns for each date, but need to produce this report quite regularly, for different date ranges each time!
I still want to use SSRS to deliver the results to the user at the end. It would just help if the query behind that report already had a column for each date.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, January 11, 2013 3:50 AM
Points: 12,
Visits: 82
|
|
Hi Chris
The bummer is it's a different date range each time! Ideally I'd like whatever code I use to get the date range in question first, i.e. find the min check-in date and max check-out date from the dataset, and show date columns accordingly.
I didn't think it would be easy!
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:37 AM
Points: 921,
Visits: 3,748
|
|
nick-1043370 (1/6/2012)
Hi Chris The bummer is it's a different date range each time! Ideally I'd like whatever code I use to get the date range in question first, i.e. find the min check-in date and max check-out date from the dataset, and show date columns accordingly. I didn't think it would be easy! 
It's surprisingly easy when you have sample data to work with 
Please could you read the article linked to in my sig, Nick? You're new here - welcome aboard - the article will explain to you how best to pose a question for a speedy and accurate reply (which you've done) and how to set up easily-consumable sample data.
Cheers
Low-hanging fruit picker and defender of the moggies
For better assistance in answering your questions, please read this.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923,
Visits: 26,811
|
|
nick-1043370 (1/6/2012) Hi
I need to take a set of records for people with hotel check-in/check-out dates, and display a table that has a column for each date in the range, with a 1 if they need a room that night, or 0 if they don't.
e.g. PersonID,Name,Checkin,Checkout 1,Fred,2012-01-02,2012-01-05 2,Bob,2012-01-01,2012-01-04
would produce the result:
PersonID,Name,Jan 1 2012,Jan 2 2012,Jan 3 2012,Jan 4 2012,Jan 5 2012 1,Fred,0,1,1,1,0 2,Bob,1,1,1,0,0
(people need a room on the night they check in, but not on the night they check out).
I'm not sure where to start! It looks like something that pivots are meant to address, but don't fully understand them yet, a stored proc or a table-value function. I'm inclined to think the table-value function would give me the most flexibility as I could join PersonID to other tables to get more data about the people if needed.
Any suggestions/similar sample code would be much appreciated.
Nick
Ok... I'm confused. People would almost always need the room for the night they checkin and never need a room for the night they checkout. Why would BOB not need his room for the last two nights and why would Fred not need his room on the day of Checkin (according to the data you posted).
Also, where are you getting THAT bit of information from?
--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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 4:22 PM
Points: 10,990,
Visits: 10,542
|
|
USE tempdb -- Test data CREATE TABLE dbo.Reservations ( PersonID integer NOT NULL, CheckIn date NOT NULL, CheckOut date NOT NULL, CHECK (CheckOut >= CheckIn) ) GO INSERT dbo.Reservations (PersonID, CheckIn, CheckOut) VALUES (1, {D '2012-01-02'}, {D '2012-01-05'}), (2, {D '2012-01-01'}, {D '2012-01-04'})
CREATE TABLE #Unpivoted ( PersonID integer NOT NULL, TheDate date NOT NULL, PRIMARY KEY (TheDate, PersonID), UNIQUE (PersonID, TheDate) );
-- Standard in-line numbers table WITH N1 AS (SELECT N1.n FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) AS N1 (n)), N2 AS (SELECT L.n FROM N1 AS L CROSS JOIN N1 AS R), N3 AS (SELECT L.n FROM N2 AS L CROSS JOIN N2 AS R), N4 AS (SELECT L.n FROM N3 AS L CROSS JOIN N3 AS R), Numbers AS (SELECT ROW_NUMBER() OVER (ORDER BY @@SPID) AS n FROM N4) -- Unpivot so there is one row per reservation date per person INSERT #Unpivoted (PersonID, TheDate) SELECT R.PersonID, Occupied.TheDate FROM dbo.Reservations AS R CROSS APPLY ( SELECT TOP(DATEDIFF(DAY, R.CheckIn, R.CheckOut)) DATEADD(DAY,Numbers.n - 1, R.CheckIn) FROM Numbers ORDER BY Numbers.n ) AS Occupied (TheDate)
-- Dynamic SQL for the cross-tab DECLARE @SQL nvarchar(max) = N'SELECT U.PersonID ' + ( -- Construct the dynamic pivot SELECT N',SUM(CASE WHEN U.TheDate = ''' + CA.date_string + N''' THEN 1 ELSE 0 END) AS ' + QUOTENAME(CA.date_string) FROM ( -- Unique dates in the unpivoted set SELECT U.TheDate, CONVERT(char(8), U.TheDate, 112) AS date_string FROM #Unpivoted AS U GROUP BY U.TheDate ) AS CA ORDER BY CA.TheDate FOR XML PATH (''), TYPE ).value('./text()[1]', 'NVARCHAR(MAX)') + N' FROM #Unpivoted AS U GROUP BY U.PersonID'
-- For debugging SELECT @SQL
-- Return results EXECUTE (@SQL)
-- Tidy up DROP TABLE #Unpivoted DROP TABLE dbo.Reservations Output:
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 8:52 AM
Points: 509,
Visits: 388
|
|
Jeff Moden (1/8/2012) Ok... I'm confused. People would almost always need the room for the night they checkin and never need a room for the night they checkout. Why would BOB not need his room for the last two nights and why would Fred not need his room on the day of Checkin (according to the data you posted).
Jeff, The OP's data is fine, as is his result set. The thing here is that he wants a column for each day in the report range, whether people were using rooms or not. Thus the 1's and 0's in his output data are correct - Fred checked in on 2nd, NOT the first day of the report.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923,
Visits: 26,811
|
|
Agh! I totally misread the dates, Bob. Thank you for the correction. More coffee, please. 
With that little revelation, it looks like Paul sussed the problem with his 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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|