Jay
Jay

Reputation: 51

HIVE Pivot and Sum

I have a table that I am trying to figure out how to pivot and sum based on the values in a second column.

Example input:

|own|pet|qty|
|---|---|---|
|bob|dog| 2 |
|bob|dog| 3 |
|bob|dog| 1 |
|bob|cat| 1 |
|jon|dog| 1 |
|jon|cat| 1 |
|jon|cat| 1 |
|jon|cow| 4 |
|sam|dog| 3 |
|sam|cow| 1 |
|sam|cow| 2 |

Example output:

|own|dog|cat|cow|
|---|---|---|---|
|bob| 6 | 1 |   |
|jon| 1 | 2 | 4 |
|sam| 1 |   | 3 |

Upvotes: 5

Views: 10930

Answers (2)

David דודו Markovitz
David דודו Markovitz

Reputation: 44921

For dynamic data you can use MAP

select      own
           ,str_to_map(concat_ws(',',collect_list(concat(pet,':',cast(qty as string))))) as pet_qty

from       (select      own,pet
                       ,sum(qty) qty 

            from        mytable

            group by    own,pet
            ) t

group by    own
;

+-----+---------------------------------+
| own |             pet_qty             |
+-----+---------------------------------+
| bob | {"cat":"1","dog":"6"}           |
| jon | {"cat":"2","cow":"4","dog":"1"} |
| sam | {"cow":"3","dog":"3"}           |
+-----+---------------------------------+

Upvotes: 2

leftjoin
leftjoin

Reputation: 38290

Use case and sum():

select own, sum(case when pet='dog' then qty end) as dog,
            sum(case when pet='cat' then qty end) as cat,
            sum(case when pet='cow' then qty end) as cow
  from your_table
 group by own;

Upvotes: 9

Related Questions