Postgres: convert single row to multiple rows (unpivot)

A single SELECT with a LATERAL join to a VALUES expression does the job of “un-pivoting” columns to separate rows: SELECT p.id, v.* FROM price_list p CROSS JOIN LATERAL ( VALUES (‘type_a’, p.price_type_a) , (‘type_b’, p.price_type_b) , (‘type_c’, p.price_type_c) ) v (price_type, price); Related: Convert one row into multiple rows with fewer columns SELECT DISTINCT … Read more

Hive – Unpivot functionality in hive

Whenever I want to pivot a table in Hive, I collect key:value pairs to a map and then reference each key in the next level, creating new columns. This is the opposite of that. Query: select a.userid, y.new_id from ( select new_id, fruit_name, fruit_code from ( select new_id, map(“apple_id”, apple_id , “mango_id”, mango_id , “grape_id”, … Read more

Unpivot an Excel matrix/pivot-table?

Oh, well, it’s a little complicated. One of the problems is, the wizard-callup shortcuts don’t work in non-english versions of excels (damn, at home I would have the English version, but here at work…) Here’s a good video: https://www.youtube.com/watch?v=pUXJLzqlEPk But youtube videos can be deleted, so to make it a solid SO answer: First, you … Read more