How do I name the columns of a PIVOT table to something that is not a number?
Is it a requirement that the names of the spreading columns be a number?
I'm working my way thought the book Querying Microsoft SQL Server 2012 in preparation for the 70-461 exam. I am currently on the lesson for PIVOT tables (Chapter 5, Lesson 2, Page 163). The following SQL executes successfully.
WITH Pivot234
AS
(
select ss.companyname, --Grouping column
datepart(dw, so.orderdate) as DayNumber, --Spreading column
so.freight --Aggregation Column
FROM Sales.Orders so
LEFT OUTER JOIN sales.Shippers ss ON so.shipperid = ss.shipperid
)
SELECT companyname, [1],[2],[3],[4],[5],[6],[7]
FROM Pivot234
PIVOT (AVG(FREIGHT)
FOR DayNumber IN ([1],[2],[3],[4],[5],[6],[7])
) AS p;
I want the name of my attributes to be the days of the week not numbers. When I change the numbers in the PIVOT table operator to the days of the week I get the following…
WITH Pivot234
AS
(
select ss.companyname, --Grouping column
datepart(dw, so.orderdate) as DayNumber, --Spreading column
so.freight --Aggregation Column
FROM Sales.Orders so
LEFT OUTER JOIN sales.Shippers ss ON so.shipperid = ss.shipperid
)
SELECT companyname, [Mon],[Tues],[Wed],[Thurs],[Fri],[Sat],[Sun]
FROM Pivot234
PIVOT (AVG(FREIGHT)
FOR DayNumber IN ([Mon],[Tues],[Wed],[Thurs],[Fri],[Sat],[Sun])
) AS p;
The second SQL query has the following error…
Msg 8114, Level 16, State 1, Line 36
Error converting data type nvarchar to int.
Msg 473, Level 16, State 1, Line 36
The incorrect value "Mon" is supplied in the PIVOT operator.Line 36 is FOR DayNumber IN ([Mon],[Tues],[Wed],[Thurs],[Fri],[Sat],[Sun])
What am I not understanding about PIVOT tables?
Best Answer
Alias the columns in the select. The values in the
In (...)
part of thePivot
need to be the exact field values observed in the data.The
Pivot(Sum([ValueField]) For [CategoryField] In ('Category A', 'Category B' ... ))
syntax is looking for the literal values that you supply in theIn ()
to be in the[CategoryField]
. It isn't mapping number 1 to position 1, it is searching the field for those exact values.