Converting columns to rows (UNPIVOT) in hiveql
Whoops, posted this in a hurry it seems. I have the answer. Posting it here for others who might find this useful.
Here is the correct syntax to deal with map and explode to achieve this.
select column1, column2, column3, m_key, m_val from
(select column1, column2, column3, map("X1", X1, "X2", X2, "X3", X3, "X4", X4) as map1
from table1) as t1
lateral view explode(map1) xyz as m_key, m_val
How to unpivot table in hive?
*Updated according to the OP reply for my comment (using week_number instead of week_name)
select item
,location
,pe.pos+1 as week_number
,pe.val as week_value
from mytable t
lateral view posexplode(array(week1,week2,week3,week4)) pe
;
+-------+-----------+--------------+-------------+
| item | location | week_number | week_value |
+-------+-----------+--------------+-------------+
| 1000 | 10000000 | 1 | 1.2 |
| 1000 | 10000000 | 2 | 2.2 |
| 1000 | 10000000 | 3 | 3.2 |
| 1000 | 10000000 | 4 | 4.5 |
| 1001 | 10000001 | 1 | 1.8 |
| 1001 | 10000001 | 2 | 2.5 |
| 1001 | 10000001 | 3 | 3.5 |
| 1001 | 10000001 | 4 | 4.1 |
| 1002 | 10000002 | 1 | 9.3 |
| 1002 | 10000002 | 2 | 2.9 |
| 1002 | 10000002 | 3 | 3.7 |
| 1002 | 10000002 | 4 | 4.8 |
+-------+-----------+--------------+-------------+
How to transpose/pivot data in hive?
Here is the solution I ended up using:
add jar brickhouse-0.7.0-SNAPSHOT.jar;
CREATE TEMPORARY FUNCTION collect AS 'brickhouse.udf.collect.CollectUDAF';
select
id,
code,
group_map['p'] as p,
group_map['q'] as q,
group_map['r'] as r,
group_map['t'] as t
from ( select
id, code,
collect(proc1,proc2) as group_map
from test_sample
group by id, code
) gm;
The to_map UDF was used from the brickhouse repo: https://github.com/klout/brickhouse
Related Topics
Counting the Number of Occurrences of a Substring Within a String in Postgresql
How to Use a Dynamic Parameter in a in Clause of a JPA Named Query
Functions with Variable Number of Input Parameters
How to Create a Decimal Field in Access with Alter Table
Postgresql 9.3: Dynamic Pivot Table
Primary Key/Foreign Key Naming Convention
SQL Insert into from Multiple Tables
What's the Correct Name for an "Association Table" (A Many-To-Many Relationship)
How to Define a Trigger on Commit in Oracle
Adding Primary Key to SQL View
The Network Adapter Could Not Establish the Connection - Oracle 11G
Concurrency Handling of SQL Transactrion
SQL Server Search Using Like While Ignoring Blank Spaces
Check If Table Exists and If It Doesn't Exist, Create It in SQL Server 2008
How to Export Data from Excel Spreadsheet to SQL Server 2008 Table