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 |
seplor
Starting Member
2 Posts |
Posted - 2014-09-05 : 16:22:57
|
I'm need help to split a row into multiple rows based on multiple column values.time_id = 111employee_id = 222time_in = 10:00time_out = 16:00break1_in = 12:00break1_out = 13:00break2_in = 14:00break2_out = 15:00I would like to break this into multiple time_in/time_out based on if they have breaks. Breaks are not required and will come across blank if non are taken.row 1time_in 10:00time_out 12:00row 2time_in 13:00time_out 14:00row 3time_in 15:00time_out 16:00 Any help would be appreciated. |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-09-06 : 06:40:11
|
[code];with cte as( select time_id, employee_id, [time] = time_in from [table] union all select time_id, employee_id, [time] = time_out from [table] union all select time_id, employee_id, [time] = break1_in from [table] union all select time_id, employee_id, [time] = break1_out from [table] union all select time_id, employee_id, [time] = break2_in from [table] union all select time_id, employee_id, [time] = break2_out from [table]),cte2 as( select *, rn = row_number() over (partition by time_id, employee_id order by [time]) from cte)select time_id, employee_id, time_in = min([time]), time_out = max([time])from cte2group by time_id, employee_id, (rn - 1)/ 2[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
seplor
Starting Member
2 Posts |
Posted - 2014-09-08 : 12:50:34
|
Thanks. This has solved my problem. |
|
|
|
|
|
|
|