Home > Enterprise >  How to transpose table in presto?
How to transpose table in presto?

Time:03-30

I have a question, which results in

A  |  B | C | D | E
2. |  3 | 10| 25| 60

How to convert it to

Reason | Percentage
A.     | 2
B.     | 3
C.     | 10
D.     | 25
E.     | 60

(ignore the pots)

Thanks

CodePudding user response:

You can try to use UNNEST function.

SELECT v.*
FROM T
CROSS JOIN UNNEST(ARRAY[
 ROW('A', A), 
 ROW('B', B), 
 ROW('C', C), 
 ROW('D', D),
 ROW('E', E)
]) AS v(Reason, Percentage);

another way is using UNION ALL

SELECT 'A' Reason , A Percentage FROM T
UNION ALL
SELECT 'B' Reason , B Percentage FROM T
UNION ALL
SELECT 'C' Reason , C Percentage FROM T
//....
  • Related