I have the following query:
SELECT * FROM (
SELECT * FROM View_ExecutiveForecastReport
WHERE WORKGROUPID IN (94)
)
PIVOT (SUM(COURSELENGTH) AS LENGTH FOR (WORKGROUPID) IN (
94 as "B2B",
66 as "CFS",
69 as "800IB",
76 as "TSData",
99 as "RetailSales",
103 as "Telesales"
))
I’d like to instead write it using a single SELECT instead of nesting. I’ve tried:
SELECT * FROM View_ExecutiveForecastReport
WHERE WORKGROUPID IN (94)
PIVOT (SUM(COURSELENGTH) AS LENGTH FOR (WORKGROUPID) IN (
94 as "B2B",
66 as "CFS",
69 as "800IB",
76 as "TSData",
99 as "RetailSales",
103 as "Telesales"
))
But I get the error: ORA-00933: SQL command not properly ended
I’ve also tried:
SELECT * FROM View_ExecutiveForecastReport
PIVOT (SUM(COURSELENGTH) AS LENGTH FOR (WORKGROUPID) IN (
94 as "B2B",
66 as "CFS",
69 as "800IB",
76 as "TSData",
99 as "RetailSales",
103 as "Telesales"
))
WHERE WORKGROUPID IN (94)
Which gives me the error: ORA-00904: “WORKGROUPID”: invalid identifier
If I remove the WHERE clause, the query works fine. I’m also open to solutions that use a CTE.
This won’t work because PIVOT is part of the table_reference portion of the FROM clause table_reference, which is why this works:
Since workgroupid is not returned as a column in the PIVOT transformation, you can’t use it in the WHERE clause. The usage of SELECT * FROM (subquery) PIVOT is quite common, so I suggest you use it.