Translation specification for the transfomation of TD_UNPIVOT into a equivalent query in Snowflake
Description
TD_UNPIVOT in Teradata can unpivot multiple columns at once, while Snowflake UNPIVOT can only unpivot a single column. The unpivot functionality is used to transform columns of the specified table into rows. For more information see TD_UNPIVOT.
The following transformation is able to generate a SQL query in Snowflake that unpivots multiple columns at the same time, the same way it works in Teradata.
SELECT*FROM (SELECT myKey,TRIM(GET_IGNORE_CASE(OBJECT_CONSTRUCT('FIRSTSEMESTERINCOME', 'First', 'FIRSTSEMESTEREXPENSES', 'First', 'SECONDSEMESTERINCOME', 'Second', 'SECONDSEMESTEREXPENSES', 'Second'), Semester), '"') AS Semester,
Income,Expenses FROM superunpivottest UNPIVOT(Income FOR Semester IN (firstSemesterIncome, secondSemesterIncome)) UNPIVOT(Expenses FOR Semester1 IN (firstSemesterExpenses, secondSemesterExpenses)) WHERE Semester = 'FIRSTSEMESTERINCOME' AND Semester1 = 'FIRSTSEMESTEREXPENSES' OR Semester = 'SECONDSEMESTERINCOME' AND Semester1 = 'SECONDSEMESTEREXPENSES') ORDER BY mykey, Semester;
1.TD_UNPIVOT with INCLUDE_NULLS clause set to YES is not supported
Snowflake UNPIVOT function used in the transformation will ignore null values always, and the user will be warned that the INCLUDE_NULLS clause is not supported when it is set to YES.
2. Table information is required to correctly transform the function
SnowConvert needs the name of the columns that are being used in the TD_UNPIVOT function, if the user does not include the columns list in the query_expression of the function but provides the name of the table being unpivoted then it will try to retrieve the column names from the table definition. If the names can not be found then the user will be warned that the resulting query might be losing columns in the result.