简体   繁体   中英

How to inverse column and row in Redshift?

How can I inverse my data in Redshift from

user_id type1 type2
1 true false
2 true false

to

user_id type value
1 type1 true
1 type2 false
2 type1 true
2 type2 false

One approach uses a union all:

SELECT user_id, 'type1' AS type, type1 AS value FROM yourTable UNION ALL
SELECT user_id, 'type2', type2 FROM yourTable
ORDER BY user_id, type;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM