I am wondering how can I create Cross Tabs queries with SQL Server 2008. I have fields for Job Numbers and Employees and I want to show that how many hours an Employee has worked on the specific job.
Here is the query.
SELECT Ename, JobNum, LaborHrs
FROM CombinedLabor
which will return the list of employee name, with job nums and labor hrs.
The existing table is as follows:
ENAME JOBNUM LABORHRS
abc N6880 8.0
abc N6880 2.5
xyz N7860 9.5
... ... ...
So I want something like this
N6880 N7860 ... Total Hrs
abc 10.5 0.0 ... 10.5
xyz 0.0 9.5 ... 9.5
I tried PIVOT
but that didn't work for. Later I want to export this data to EXCEL with C#.
EDIT
Okay here is the query.
SELECT Ename, JobNum,LaborHrs FROM CombinedLabor
which will return the list of employee name, with job nums and labor hrs.
The existing table is as follows:
ENAME JOBNUM LABORHRS abc N6880 8.0 abc N6880 2.5 xyz N7860 9.5 ... ... ...
So I want something like this
N6880 N7860 ... Total Hrs abc 10.5 0.0 ... 10.5 xyz 0.0 9.5 ... 9.5
And the result
I would suggest you try doing your cross tab manipulation in C#:
http://code.google.com/p/pivot-tools/
I suspect you'll find it easier than fiddling with a dynamic pivot. (This is my project, so let me know if you have any questions.)
Before doing that, you might want to retrieve "Total Hrs" in advance, like so:
(A suggestion: Different employees could have the same name, so you should have an id column to join on.)