I wish to extract a few Calender Weeks from an yearly data. Once that's done, I want to pivot it, so that there is one row for each ID.
We have a table DB.MY_CWs having just one column CW containing the Calender Weeks we are interested in.
The following code extracts the relevant Calender Weeks.
CREATE TABLE DB.MY_TABLE AS
(
SELECT ID,
WeekNumber_Of_Year(Sales_Date)) AS CW,
AVG(Sales) AS Sales
FROM DB.DataBase_XYZ
WHERE CW IN (SELECT CW FROM DB.MY_CWs)
GROUP BY ID,CW
) WITH DATA;
This Code gives us the output like this:
But, I would like to pivot it so that I get an output like this:
I took the help from code here and ran the following, but TeraData doesn't respond and there is no Error either.
CREATE TABLE DB.MY_TABLE2 AS
(
SELECT *
FROM DB.MY_TABLE
PIVOT
(SUM(Sales) AS Sales
FOR CW IN (SELECT CW FROM DB.MY_CWs)
) AS dt
) WITH DATA;
If instead of (SELECT CW FROM DB.MY_CWs) I would have used (15,16,17), then everything works fine and I would have got the pivoted Table, as shown above.
Can anyone suggest where I am making the mistake? Many thanks.

