I am counting events from a history table and want to pivot on the month the events occur. Base query is something like this:
SELECT TO_CHAR(Date_Entered, 'MONTH') AS Month, Userid FROM Customer_Order_History
The full query that implements the PIVOT then looks like:
SELECT * FROM
(SELECT TO_CHAR(Date_Entered, 'MONTH') AS Month, Userid FROM Customer_Order_History)
PIVOT
(
COUNT(*) AS Events
FOR Month
IN ('JANUARY' AS Jan, 'FEBRUARY' AS Feb, 'MARCH' AS Mar, 'APRIL' AS Apr,
'MAY' AS May, 'JUNE' AS Jun, 'JULY' AS Jul, 'AUGUST' AS Aug,
'SEPTEMBER' AS Sep, 'OCTOBER' AS Oct, 'NOVEMBER' AS Nov, 'DECEMBER' AS Dec)
)
This query is valid and runs fine. Except in the results, where I should have had events to count for July, August, and September, everything is zeroes except September.
The problem was with the literal values in the IN() clause of the PIVOT. I come from a FoxPro background where the length of a string data column is not variable, so "JULY" is not the same as "JULY " (JULY plus five spaces). On a hunch, then, I changed the literals in the IN() to all be nine characters in length (the longest a month name can be), and July and August began reporting counts.
Of course, the better way to do this is to use a more consistent format for the month, such as just using the first three characters or using the month index. But the above-described behavior seems very odd to me. After all, if I compared a VARCHAR(100) with a VARCHAR(2000), and they were both "JULY", in a WHERE clause or a JOIN, they would be considered equal. I am used to padding strings to a certain width when comparing in FoxPro -- not in Oracle.
Has anyone else seen this in PIVOTs, and is there some larger concept I am missing in Oracle where the size of a VARCHAR column suddenly starts to matter (other scenarios I should be aware of)?