SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-03 : 15:41:57
|
[code]-- prepare sample datadeclare @t table (orderno int, date1 datetime, date2 datetime, statusbefore varchar, statusafter varchar)insert @tselect 1, '08-10-2006', '09-10-2006', 'a', 'b' union allselect 1, '09-10-2006', '10-10-2006', 'b', 'c' union allselect 1, '11-10-2006', '12-10-2006', 'c', 'd' union allselect 2, '08-10-2006', '09-10-2006', 'a', 'b' union allselect 2, '09-10-2006', '10-10-2006', 'b', 'c' union allselect 2, '11-10-2006', '12-10-2006', 'c', 'd' union allselect 3, '08-10-2006', '09-10-2006', 'a', 'b' union allselect 3, '09-10-2006', '10-10-2006', 'b', 'c' union allselect 3, '11-10-2006', '12-10-2006', 'c', 'd'-- show the final resultselect orderno, max(case when statusbefore = 'a' and statusafter = 'b' then datediff(day, date1, date2) end) 'a-b', max(case when statusbefore = 'b' and statusafter = 'c' then datediff(day, date1, date2) end) 'b-c', max(case when statusbefore = 'c' and statusafter = 'd' then datediff(day, date1, date2) end) 'c-d'from @tgroup by ordernoorder by orderno[/code]Peter LarssonHelsingborg, Sweden |
 |
|