Straight Pivot with 2nd Column as Values for the 1st Column Values

dbt related question:

Anyone have an idea on creating a pivot chart using a second column as the value for the first in dbt? Tried dbt.utils.pivot but not finding any evidence that the macro supports this function and will return anything other than boolean or aggregates.

Thought of creating my own macro that queries the second column data but stuck on how to use a case statement to pivot, rename, and set the data type (needs to be varchar).

In layman’s terms -
I have columns: redcord_id, field_names, field_values, export_date and need to pivot so the the values in the column field_names will be the columns on the new table along with record_id, and export_date, and each value from field_names will be filled with data from field_values.

There are more than 20 values for field_names so avoiding cast and trying for a more dynamic approach that follows the get_column_values pattern.

Managed to figure this out. No worries! Managed to figure it out.

Took some adjustments from this article which really set up the macro, but managed it: https://serge-g.medium.com/dynamic-sql-pivots-with-dbt-dea16d7b9b63|https://serge-g.medium.com/dynamic-sql-pivots-with-dbt-dea16d7b9b63

Note: @alexnleyva97 originally posted this reply in Slack. It might not have transferred perfectly.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.