How to PIVOT
You may have a use case that requires pivoting. Pivoting rotates a table by turning the unique values from one column in the input expression into multiple columns and aggregating results where required on any remaining column values. For example:
See Snowflake’s PIVOT documentation for more information on this functionality.
In Coalesce, PIVOT can be achieved via two differing approaches:
- Stage Node
- View Node with the Override Create SQL option enabled in the Node’s configuration
The approach you select will depend on your and your organization's preferences. You will still have to craft the PIVOT SQL as detailed in Snowflake’s PIVOT documentation under both approaches.
Pivot Using a Stage Node
-
In the Browser of the Build Interface, ensure you have added the Node (Source, Stage, etc.) that you will perform the pivot on.
-
Right click on the Node that you will perform the pivot on and select Add Node > Stage.
-
In the Mapping tab of the Node Editor for the Stage, re-configure your column names, transforms, and data types to reflect what will be returned by the PIVOT statement. This should include your row columns, in order, followed by the pivoted values. Leave the Source for each column blank, and fill in the Transform for each column with the column name as it will be returned from the SQL; note that Snowflake encloses pivoted column names in single quotes, for example, JAN will be named 'JAN' in the SQL results; to correct for this, you will override the returned named with your intended names in the JOIN configuration described in Step 4. For example:
-
In the Stage pictured below, the results will result-set will return three columns, STORE, AMOUNT, and MONTH.
-
If you wish to pivot AMOUNT on MONTH and group by STORE, you'd reconfigure your stage as pictured:
-
-
Navigate to the Join tab of the Node Editor and update your join to include your PIVOT clause after your FROM clause. For the example detailed above, this PIVOT clause would be PIVOT(SUM(AMOUNT) FOR MONTH IN ('JAN', 'FEB', 'MAR', 'APR')) AS p (STORE, JAN, FEB, MAR, APR) as pictured in the example below.
-
Within the Node Editor:
- Validate Select.
- Validate Create.
- Create.
- Validate Run.
- Fetch data, and view pivoted results.
-
Proceed to add the dependent downstream Nodes for your data pipeline, whether it be direct to target or additional processing Nodes. For example:
Pivot Using a View Node With Override Create SQL
-
In the Build Settings of your Workspace in the Coalesce app, enable the View Node Type if it is not already enabled for your workspace. This Node Type is disabled by default.
-
In the Browser of the Build Interface, ensure you have added the Node (Source, Stage, etc.) that you will perform the pivot on.
-
Right click on the Node that you will perform the pivot on and select Add Node > View.
-
In the View Node’s configuration settings, toggle on the Override Create SQL option.
-
Navigate to the Create SQL tab of the Node Editor, and modify the SQL as follows:
-
Replace the existing column list with asterisk (*). For example, if my SQL looks like the construct pictured below:
-
You can modify it by replacing my column list of STORE AS STORE, AMOUNT AS AMOUNT, MONTH AS MONTH with a simple asterisk (*) as pictured below.
-
Update your join to include your PIVOT clause after your FROM clause. For the example detailed above, this PIVOT clause would be PIVOT(SUM(AMOUNT) FOR MONTH IN ('JAN', 'FEB', 'MAR', 'APR')) AS p (STORE, JAN, FEB, MAR, APR) as pictured in the example below.
-
-
Still in the Create SQL tab of the View Node Editor, click the Derive Mapping button. This will detect the changes between the Node’s current column configuration vs. those found in the Create SQL you’ve input, and ask you to confirm you wish the changes to take effect. If everything looks correct, click Update.
-
Navigate back to the mapping tab, and confirm the column changes and mappings have taken effect.
-
Within the Node Editor:
- Validate Select.
- Validate Create.
- Create.
-
Proceed to add the dependent downstream Nodes for your data pipeline, whether it be direct to target or additional processing Nodes. For example: