sql server 2008 - Finding within a SQL dataset the most recent period in which a hotel had at least one person staying -


I have a data that includes an ID for the hotel, check-in date and check-out date. I am trying to find out the most recent period for each hotel where at least one person was staying in the hotel every night during this period.

An example shown below is a dataset:

  Right to create (Hotel ID, Individual Identity, Checkin, CheckOut) Price (1, 1, '20 / January / 2015 (1, 2, '13 / January / 2015 ',' 20 / January / 2015 ') Enter the occupation (Hotel ID,' 22 '), surcharge (Hotel ID INT, Individual Identity INT, Checkin DATE, CheckOut DATE) (1, 3, '20 / January / 2015 ',' 22 / January / 2015 ') Enter the acquisition price (Hotel ID, Individual Identity, Checkin, Checkout) Price (1, 4,' Parsari Id, Check In, etc.) 12 (1, 6, '01 / 01 ') (1, 5, '01 / January / 2015', '10 / Jan / 2015') Enter the occupation (Hotel ID, Personal ID, Checkin, CheckOut) January / 2015 ',' 04 / January / 2015 ') Occup in occupation (Hotel ID, PersonID, Checkin, Checkon) VA Luse (2, 7, '10 / January / 2015', '20 / January / 2015') Occupation Sign in (Hotel ID, PersonID, Checkin, CheckOut) Price (2, 8, '11 / January / 2015 ', '12 (Hotel ID, PersonID, Checkin, CheckOut) prices (2, 9, '12 / January / 2015) ',' 13 / Jan / 201 ('Hotel ID, Personal ID, Check In, etc.) Price (2, 10, '12 / January / 2015', '13 / January / 2015') in occupancy (Hotel ID, Personal ID, Checkin, CheckOut) Entries (2, 11, '01 / January / 2015 ') (3, 12, '02 / January / 2015', '03 / January / 2015') Occupation (Hotel ID, Personal ID, Insurance, Insurance, Value (3, 13) , '04 / Jan / 2015 ',' 05 / January / 2015 ') Enter the occupation (Hotel ID, Personal ID, Checkin, CheckOut) Price (3, 14, '05 / January / 2015)  

I am trying to create a scene and I am hoping the results are as follows:

  from HotelID to 12 January / 2015 22 / January / 2015 2 January 10 / I suspect that you need to do a recursive query, but I'm not sure how to add to me: 2015 20 / January / 2015 3 04 / January / 2015 06 / January / 2015  

I'm using Microsoft SQL Server 2008 R2 thanks for any help already.

I think I got the answer, though it is quite complex. I suspect there may be a better way here:

 with  as overlapping obligation (Select * from (select Row_number ()) (checkout DESC, by checkin ASC As per AAO number, Hotel ID, Checkin, Checkon overload from Hotel Id order) R rank number = 1 union Select all * from (Select Row_number ()) by participation (occupancy by house. Accommodation order by residence. Checkin ASC ) In form of Occupation: Occupation: Occupation: Occupation: Occupation: Occupation Occupation: Occupancy check: Occupancy Occupancy from INNER overlapping include Overlapping Occupancy: hollyd = Occupation hollyide where domicile checks> Check Overlapping Occupation Check & Occupancy Check & amp; Overlapping Incorporation. Check in) AS RankedOccupancy Where RowNumbe Hotel ID from Overlapping Occupancy Group  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -