Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Tranfrorm data from columns to rows

Author  Topic 

SuzyB_24
Starting Member

11 Posts

Posted - 2012-12-11 : 09:06:42
I have two tables, one with a list of columns relating to the hotels that a tourist will stay on during their trip and the other the details of the hotel.

This is a sample of the trip data

Id HotelOne HotelOneDuration HotelOneCheckIn HotelTwo HotelTwoDuration HotelTwoCheckIn HotelThree HotelThreeDuration HotelThreeCheckIn
44 129 3 2012-12-15 127 5 2012-12-19 No Hotel 0 0001-01-01

And this the hotel data.

Id HotelName HotelImage HotelText Location StarRatiing
129 Marriott pic.jpg xxx 1 3
127 Golf Resort golf-resort.jpg yyy 1 5


What I'd like to do is pull the hotel data out in rows if possible. Something like this

Position HotelName HotelText Duration CheckIn
1 Marriott xxx 3 2012-12-15
2 Golf Resort yyy 5 2012-12-19


Is it possible to pull the data out the database like this. Or should I just sort it out in the code.

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-11 : 09:10:23
Check for UNPIVOT or Union all
Go to Top of Page
   

- Advertisement -