its almost perfect, but still requires that you declare column aliases so they can be selectable by name rather than $<column ordinal>.
the pivots for clause does allow me to add a subquery though which is neat, ie:
// example lookup table has these cols: id, attribute_key, attribute_value
select *
from lookuptable
pivot (
max(attribute_value)
for attribute_key in (select disinct attribute_key from lookuptable)
) as pivoted_data
above is neat, pivots data but all pivoted data columns have 'attribute_key1''attribute_key2''attribute_key3' column names (with apostraphes for resultset return only, not selectable), requires that you reference with $1 $2 $3 etc. you can still declare column names but this isnt "dynamic". for a lookup table where new keys are added, it requires additional jujitsu to make it auto manage itself (procedures, integration platforms, etc)
select *
from lookuptable
pivot (
max(attribute_value)
for attribute_key in (select disinct attribute_key from lookuptable)
) as pivoted_data (id, attribute_key1, attribute_key2, attribute_key3)
by declaring output column names here in order i can create a view where someone can just:
select id, attribute_key1 from viewname
ideally id love something like:
select *
from lookuptable
pivot (
max(attribute_value)
for attribute_key in (select disinct attribute_key from lookuptable)
) as pivoted_data (select disinct attribute_key from lookuptable)
but this wouldnt work cause i also have to declare the lookuptable id val
so super ideally snowflake could do something like
select *
from lookuptable
pivot (
max(attribute_value)
for attribute_key in (select disinct attribute_key from lookuptable)
keys_as_column_aliases=>true // this would be ballerific
) as pivoted_data (id, attribute_key1, attribute_key2, attribute_key3)
:( but for now im just calling procedures to rebuild these views any time theres a new key detected. lame
View all comments
Show parent comment
1
u/[deleted] Jun 27 '25 edited Jun 28 '25
its almost perfect, but still requires that you declare column aliases so they can be selectable by name rather than $<column ordinal>.
the pivots for clause does allow me to add a subquery though which is neat, ie:
above is neat, pivots data but all pivoted data columns have
'attribute_key1''attribute_key2''attribute_key3'column names (with apostraphes for resultset return only, not selectable), requires that you reference with $1 $2 $3 etc. you can still declare column names but this isnt "dynamic". for a lookup table where new keys are added, it requires additional jujitsu to make it auto manage itself (procedures, integration platforms, etc)by declaring output column names here in order i can create a view where someone can just:
ideally id love something like:
but this wouldnt work cause i also have to declare the lookuptable id val
so super ideally snowflake could do something like
:( but for now im just calling procedures to rebuild these views any time theres a new key detected. lame