Click here to Skip to main content
15,922,419 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,

I have the following excel table which contains schedule data for our employees, I import the table into our database as is(1st row as columns) and then I use the table to display data in web and user applications.

<img src="https://lh4.googleusercontent.com/-dUl0EarsTs4/TfdcgpkSnMI/AAAAAAAAAOs/xcoy0MXBU1I/s144/tablesample.gif" height="43" width="144" />

But the data formatted in this way is not very easy to query or work with in general. I would like to somehow manipulate this data into the following formatted table.

https://lh6.googleusercontent.com/-hAasVFQrxQo/TfuaQJDGuqI/AAAAAAAAAPE/Mnd1kXYb7EM/ttable2.png

I know it could be possible with a query to format the data when I Import it the 1st time from excel but the answer is not coming to me.
thanks,

skatebkp
Posted
Updated 18-Jun-11 6:19am
v6

In SQL server you can use PIVOT - UNPIVOT

Using PIVOT and UNPIVOT[^]

Pivoting data in SQL Server[^](Alternate ways)
 
Share this answer
 
Comments
skatebkp 18-Jun-11 12:22pm    
thanks for the quick reply, I think the unpivot function is exactly what I am looking for.
The following query displays the data exactly as I want it:

SELECT Agent, Date, Shift
FROM
(SELECT Agent,[01/06/11],[02/06/11],[03/06/11],[04/06/11]) from shiftTable


UNPIVOT
(Shift FOR Date IN

([01/06/11],[02/06/11],[03/06/11],[04/06/11])
)as unpvt;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900