My company just switched from Microsoft SQL Server to Snowflake, and I have a bunch of scripts where I unpivoted a wide table to a long one using code like this:
SELECT
CONGLOM_IDc,
DESTINATION_IDc,
COHORTc,
COHORT_PASS_TYPEc,
ACCESS_SEASON,
PASS_TYPE,
First_Cohortc
FROM #PassTypeByYear2 M
CROSS APPLY
(
VALUES
(CONGLOM_ID, DESTINATION_ID, COHORT, COHORT_PASS_TYPE,'18/19', "18/19_Pass", First_Cohort),
(CONGLOM_ID, DESTINATION_ID, COHORT, COHORT_PASS_TYPE,'19/20', "19/20_Pass", First_Cohort),
(CONGLOM_ID, DESTINATION_ID, COHORT, COHORT_PASS_TYPE,'20/21', "20/21_Pass", First_Cohort),
(CONGLOM_ID, DESTINATION_ID, COHORT, COHORT_PASS_TYPE,'21/22', "21/22_Pass", First_Cohort),
(CONGLOM_ID, DESTINATION_ID, COHORT, COHORT_PASS_TYPE,'22/23', "22/23_Pass", First_Cohort)
) c (CONGLOM_IDc, DESTINATION_IDc, COHORTc, COHORT_PASS_TYPEc, ACCESS_SEASON, PASS_TYPE, First_Cohortc)
Is there a way to replicate this in Snowflake without using UNPIVOT a bunch of times? I have read the following post, but I can't seem to adjust CROSS JOIN to this situation. Any help is much appreciated.
An example of the "starting" table (#PassTypeByYear2) is below:
CONGLOM_ID | DESTINATION_ID | COHORT | COHORT_PASS_TYPE | 18/19_Pass | 19/20_Pass | 20/21_Pass | 21/22_Pass | 22/23_Pass | First_Cohort | Consistent_Pass_All_Years |
---|---|---|---|---|---|---|---|---|---|---|
101781679 | 196 | 22/23 | Paid | Employee | Employee | Employee | Paid | Paid | NO | No |
101781679 | 196 | 21/22 | Paid | Employee | Employee | Employee | Paid | Paid | NO | No |
101781679 | 196 | 20/21 | Employee | Employee | Employee | Employee | Employee | Paid | NO | No |
101781679 | 196 | 18/19 | Employee | Employee | Employee | Employee | Employee | Paid | YES | No |
101781679 | 196 | 19/20 | Employee | Employee | Employee | Employee | Employee | Paid | NO | No |
101781679 | 227 | 19/20 | Employee | NULL | Employee | NULL | NULL | NULL | YES | Yes |
and the output I want is below (EDIT: I just realized I did a SELECT TOP 10 * from this table instead of *. It should have a row for every unique cominbation of unpivoted values. I will re-run this query and upload the full table tomorrow - the table is about 5M rows so it takes a long time to run):
CONGLOM_ID | DESTINATION_ID | COHORT | COHORT_PASS_TYPE | ACCESS_SEASON | PASS_TYPE | First_Cohort | Consistent_Pass_All_Years |
---|---|---|---|---|---|---|---|
101781679 | 196 | 22/23 | Paid | 18/19 | Employee | NO | No |
101781679 | 196 | 22/23 | Paid | 19/20 | Employee | NO | No |
101781679 | 196 | 22/23 | Paid | 20/21 | Employee | NO | No |
101781679 | 196 | 22/23 | Paid | 21/22 | Paid | NO | No |
101781679 | 196 | 22/23 | Paid | 22/23 | Paid | NO | No |
101781679 | 196 | 20/21 | Employee | 18/19 | Employee | NO | No |
101781679 | 196 | 20/21 | Employee | 19/20 | Employee | NO | No |
101781679 | 196 | 20/21 | Employee | 20/21 | Employee | NO | No |
101781679 | 196 | 20/21 | Employee | 21/22 | Paid | NO | No |
101781679 | 196 | 20/21 | Employee | 22/23 | Paid | NO | No |
CROSS APPLY in Oracle/SQL Server substitute in Snowflake
I tried converting the rest of the code to SNOWFLAKE, and I expected that I would end up with a long table (instead of a wide one) where the "18/19_Pass" Column turned into a value in the "18/19" ACCESS_SEASON row. Instead, I am just hitting an error:
SQL Error [2014] [22000]: SQL compilation error: Invalid expression [M.CONGLOM_ID] in VALUES clause