Skip to content

Instantly share code, notes, and snippets.

@albertywu
Last active March 26, 2021 19:40
Show Gist options
  • Select an option

  • Save albertywu/587dccf15800579adbf2761ca19b844f to your computer and use it in GitHub Desktop.

Select an option

Save albertywu/587dccf15800579adbf2761ca19b844f to your computer and use it in GitHub Desktop.
unpivot a table in presto
Convert table t1 of this form:

id | p50 | p75 | p95
---------------------
1  | 10  | 11  | 12
2  | 13  | 14  | 15

into this form:

id |  key  |  value
-------------------
1  |  p50  |  10
1  |  p75  |  11
1  |  p95  |  12
2  |  p50  |  13
2  |  p75  |  14
2  |  p95  |  15
select t1.id, t2.key, t2.value
from t1
cross join unnest (
  array['P50', 'P75', 'P95'],
  array[P50, P75, P95]
) t2 (key, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment