| Author |
Topic |
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2009-06-16 : 08:58:36
|
| I have the below tableList_JobsJob GroupID SequenceNo DestID WaitTime1 1 1 31 15:13:57.9501 1 2 11 15:13:57.9501 1 3 21 15:13:57.9501 1 4 41 15:13:57.9501 2 5 141 16:23:18.967 1 2 6 12 16:23:18.967 1 2 7 23 16:23:18.967 1 2 8 42 16:23:18.9671 3 9 142 18:49:03.090 1 3 10 61 18:49:03.090 1 3 11 22 18:49:03.090 1 3 12 42 18:49:03.090 1 4 13 142 19:02:52.950 1 4 14 13 19:02:52.950 1 4 15 43 19:02:52.950 2 1 1 31 21:13:57.9502 1 2 61 21:13:57.9502 1 3 22 21:13:57.9502 1 4 41 21:13:57.9502 2 5 141 21:23:18.967 2 2 6 13 21:23:18.967 2 2 7 23 21:23:18.967 2 2 8 42 21:23:18.9672 3 9 142 21:49:03.090 2 3 10 61 21:49:03.090 2 3 11 22 21:49:03.090 2 3 12 42 21:49:03.090 Result Required:JobID GroupID WaitTime Path1 Path2 Path3 Path41 1 15:13:57.950 31 11 21 411 2 16:23:18.967 141 12 23 421 3 18:49:03.090 142 61 22 421 4 19:02:52.950 142 13 43 NULL2 1 21:13:57.950 31 61 22 412 2 21:23:18.967 141 13 23 422 3 21:49:03.090 142 61 22 42I have made the below query:SELECT [JobID] ,[GroupID] ,[WaitTime] ,max(case [SequenceNo] when 1 then [DestID] else NULL end) as Path1,max(case [SequenceNo] when 2 then [DestID] else NULL end) as Path2,max(case [SequenceNo] when 3 then [DestID] else NULL end) as Path3,max(case [SequenceNo] when 4 then [DestID] else NULL end) as Path4FROM [iCA].[dbo].[List_Jobs] group by [JobID], [GroupID] ,[WaitTime]I need to make when 1,2,3,4 change to Minimum of SequenceNo of the GroupAny suggestions , You can please suggest any other method |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-06-16 : 11:30:24
|
[code]declare @List_Jobs table( Job int, GroupID int, SequenceNo int, DestID int, WaitTime datetime)insert into @List_Jobsselect 1, 1, 1, 31, '15:13:57.950' union allselect 1, 1, 2, 11, '15:13:57.950' union allselect 1, 1, 3, 21, '15:13:57.950' union allselect 1, 1, 4, 41, '15:13:57.950' union allselect 1, 2, 5, 141, '16:23:18.967' union allselect 1, 2, 6, 12, '16:23:18.967' union allselect 1, 2, 7, 23, '16:23:18.967' union allselect 1, 2, 8, 42, '16:23:18.967' union allselect 1, 3, 9, 142, '18:49:03.090' union allselect 1, 3, 10, 61, '18:49:03.090' union allselect 1, 3, 11, 22, '18:49:03.090' union allselect 1, 3, 12, 42, '18:49:03.090' union allselect 1, 4, 13, 142, '19:02:52.950' union allselect 1, 4, 14, 13, '19:02:52.950' union allselect 1, 4, 15, 43, '19:02:52.950' union allselect 2, 1, 1, 31, '21:13:57.950' union allselect 2, 1, 2, 61, '21:13:57.950' union allselect 2, 1, 3, 22, '21:13:57.950' union allselect 2, 1, 4, 41, '21:13:57.950' union allselect 2, 2, 5, 141, '21:23:18.967' union allselect 2, 2, 6, 13, '21:23:18.967' union allselect 2, 2, 7, 23, '21:23:18.967' union allselect 2, 2, 8, 42, '21:23:18.967' union allselect 2, 3, 9, 142, '21:49:03.090' union allselect 2, 3, 10, 61, '21:49:03.090' union allselect 2, 3, 11, 22, '21:49:03.090' union allselect 2, 3, 12, 42, '21:49:03.090'-- Query;with data (Job, GroupID, WaitTime, DestID, row_no)as( select Job, GroupID, WaitTime, DestID, row_no = row_number() over (partition by Job, GroupID order by SequenceNo) from @List_Jobs) select Job, GroupID, WaitTime, Path1 = [1], Path2 = [2], Path3 = [3], Path4 = [4]from data pivot ( max(DestID) for row_no in ([1], [2], [3], [4]) ) p[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|