Sometimes called unpivoting, here is one example of how to rotate column data with Presto.
Suppose we have some integer data arranged in two columns called a and b:
We want to rotate the columns into rows like this, where for every ‘a’ column value we now see a row labeled ‘a’, and ditto for the b values:
'a', 9 'b', 8 'a', 7
To rotate from columns to rows in Presto we’ll use a CTE and VALUES() to supply the test data, and simple conditional CASE WHEN END logic coupled with a sub-select and a UNION:
presto:default> with testdata(value_a, value_b) as ( VALUES (9,null), (null,8), (7,null) ) select key, value from (select 'a' as key, value_a as value from testdata UNION select 'b' as key, value_b as value from testdata) where value is not null; key | value -----+------- a | 9 b | 8 a | 7 (3 rows)
There are other SQL options for rotating (unpivoting) columns into rows: The CROSS JOIN unnest function is similar to LATERAL VIEW explode function.
The code sample and description here should help when you need to rotate data from columns to rows using Presto.