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.
| Author |
Topic |
|
Ranjit.ileni
Posting Yak Master
183 Posts |
Posted - 2011-03-25 : 23:30:51
|
| Hi,Here is table DDLcreate table #temp( flightname varchar(20),segmentname varchar(20),depatureplace varchar(20),arrivalplace varchar(20))insert into #temp select 'KL123','kl123a','aaaa','bbbb'union allselect 'KL123','kl123b','bbbb','cccc'union allselect 'KL123','kl123c','cccc','dddd'union allselect 'KL126','kl126a','aaaa','bbbb'union allselect 'KL126','kl126b','bbbb','dddd'union allselect 'KL128','kl128','aaaa','dddd'select * from #tempflightname segmentname depatureplace arrivalplaceKL123 kl123a aaaa bbbbKL123 kl123b bbbb ccccKL123 kl123c cccc ddddKL126 kl126a aaaa bbbbKL126 kl126b bbbb ddddKL128 kl128 aaaa ddddhere flight source is aaaa and destination is ddddKL123 flight has 3 segments(aaaa-bbbb ,bbbb-cccc,cccc-dddd)KL126 flight has 2 segments(aaaa-bbbb ,bbbb-dddd)KL128 has only one segment(aaaa-dddd)segment means Break journeysExpected output:flightname,seg1_segmentname,seg1_depatureplace,seg1_arrivalplace,seg2_segmentname,seg2_depatureplace,seg2_arrivalplace,seg3_segmentname,seg3_depatureplace,seg3_arrivalplaceKL123 kl123a aaaa bbbb kl123b bbbb cccc kl123c cccc ddddKL126 kl126a aaaa bbbb kl126b bbbb dddd null null nullKL128 kl128 aaaa dddd null null null null null null here flight can has at most 10 segments.Please help me in a query.Thanks--Ranjit |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-03-26 : 16:59:17
|
I am showing one way of doing this below.There are couple of alternatives as well.One is to use the PIVOT keyword available in SQL 2005 and later. I don't particularly like it because to me the syntax looks a little non-intuitive. But, that's just me. You might want to check it out.For what I am showing below and for the PIVOT keyword, you will need to know the cross-tab columns in advance (which in your case you do). If that is not the case, you can use dynamic cross tab, discussed here: http://www.sqlteam.com/article/dynamic-cross-tabs-pivot-tables or http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspxwith cte as( select *, row_number() over (partition by flightname order by segmentname) as N1 from #temp)select flightname, max(case when N1 = 1 then segmentname end) as seg1_segmentname, max(case when N1 = 1 then depatureplace end) as seg1_depatureplace, max(case when N1 = 1 then arrivalplace end) as seg1_arrivalplace, max(case when N1 = 2 then segmentname end) as seg2_segmentname, max(case when N1 = 2 then depatureplace end) as seg2_depatureplace, max(case when N1 = 2 then arrivalplace end) as seg2_arrivalplace --- and so on from ctegroup by flightnameorder by flightname |
 |
|
|
Ranjit.ileni
Posting Yak Master
183 Posts |
Posted - 2011-03-27 : 23:27:52
|
| Thank you so much sunitabeckIts really helpful for me--Ranjit |
 |
|
|
|
|
|
|
|