I have a table called “AllBand” with this sample data and columns.
I want to have Personnel Name in column 1 and have subsequent column headings for each date
With total hours listed under the correct Date heading
Sample raw data
Report 1|Personnel Name| Band | WE | TotalHours
Spacely, Mister| Flintstone, Fred| 7T| 11/10/2012| 40
Spacely, Mister| Flintstone, Fred| 7T| 11/10/2012| 34
Spacely, Mister| Flintstone, Wilma| 7T| 11/10/2012| 38
Spacely, Mister| Flintstone, Pebbles| 7T| 11/17/2012| 23
Sample output
Personnel Name 11/10/2012 11/17/2012
Flintstone, Fred 40 34
Flintstone, Wlima 38
Flintstone, Pebbles 23
Get error on this following code
Msg 207, Level 16, State 1, Line 2
Invalid column name 'Personnel Name'.
Msg 207, Level 16, State 1, Line 16
Invalid column name 'Personnel Name'.
DougP
I want to have Personnel Name in column 1 and have subsequent column headings for each date
With total hours listed under the correct Date heading
Sample raw data
Report 1|Personnel Name| Band | WE | TotalHours
Spacely, Mister| Flintstone, Fred| 7T| 11/10/2012| 40
Spacely, Mister| Flintstone, Fred| 7T| 11/10/2012| 34
Spacely, Mister| Flintstone, Wilma| 7T| 11/10/2012| 38
Spacely, Mister| Flintstone, Pebbles| 7T| 11/17/2012| 23
Sample output
Personnel Name 11/10/2012 11/17/2012
Flintstone, Fred 40 34
Flintstone, Wlima 38
Flintstone, Pebbles 23
Get error on this following code
Msg 207, Level 16, State 1, Line 2
Invalid column name 'Personnel Name'.
Msg 207, Level 16, State 1, Line 16
Invalid column name 'Personnel Name'.
Code:
SELECT [Personnel Name],
[11/3/2012] AS '11/3/2012',
[11/10/2012] AS '11/10/2012',
[11/17/2012] AS '11/17/2012',
[11/24/2012] AS '11/24/2012'
FROM
(SELECT [WE], [Personnel Name],[Total Hours]
from ALLBand) p
PIVOT
(
SUM([Total Hours])
FOR [Personnel Name] IN
( [11/3/2012],[11/10/2012], [11/17/2012],[11/24/2012])
) AS pvt
ORDER BY pvt.[Personnel Name];
DougP