I wonder how I might take rows from a result set and turn those rows into columns of a new table - but there is a twist - I would require the individual row items to be converted into one row of data, so that the new table would only have one row for item 3, one row for item 4 etc - but - when a new item 3 is found in the results I need to have that as a separate row in the new table.
I am using SQL Server 2012 Express. Start Date and End Date are datetime, Item is varchar(256) and Code is varchar(256):
Sample data:
Start Date End Date Item Code *2010-01-01 04:30:00 2010-01-01 04:33:35 3 1 *2010-01-01 04:33:36 2010-01-01 04:37:11 3 3 *2010-01-01 04:37:12 2010-01-01 04:40:47 3 4 2010-01-01 06:43:12 2010-01-01 06:46:47 4 2 2010-01-01 06:46:48 2010-01-01 06:50:23 4 3 2010-01-01 08:56:24 2010-01-01 08:59:59 5 2 2010-01-01 09:00:00 2010-01-01 09:03:35 5 3 2010-01-01 11:09:36 2010-01-01 11:13:11 6 2 2010-01-01 11:13:12 2010-01-01 11:16:47 6 4 *2010-01-01 14:30:00 2010-01-01 14:33:35 3 1 *2010-01-01 14:33:36 2010-01-01 14:37:11 3 2 *2010-01-01 14:37:12 2010-01-01 14:40:47 3 4
Output data (columns were there is no code would be null; above means use the start or date time as per the input rowset):
Item C1_Start C1_End C2_Start C2_End C3_Start C3_End C4_Start C4_End *3 above above null null above above above above 4 null null above above above above null null 5 null null above above above above null null 6 null null above above null null above above *3 above above above above null null above above
Thanks
Codes?