hive> select * from temp;
OK
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
Time taken: 0.102 seconds, Fetched: 8 row(s)
hive> select year,case when month = 1 then amount else 0 end as m1,case when month = 2 then amount else 0 end as m2,case when month = 3 then amount else 0 end as m3,case when month = 4 then amount else 0 end as m4 from temp;
OK
1991 1.1 0.0 0.0 0.0
1991 0.0 1.2 0.0 0.0
1991 0.0 0.0 1.3 0.0
1991 0.0 0.0 0.0 1.4
1992 2.1 0.0 0.0 0.0
1992 0.0 2.2 0.0 0.0
1992 0.0 0.0 2.3 0.0
1992 0.0 0.0 0.0 2.4
Time taken: 0.106 seconds, Fetched: 8 row(s)
hive> select year,sum(case when month = 1 then amount else 0 end) as m1,sum(case when month = 2 then amount else 0 end) as m2,sum(case when month = 3 then amount else 0 end) as m3,sum(case when month = 4 then amount else 0 end) as m4 from temp group by year;
OK
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
SQL
select year,sum(case when month = 1 then amount else 0 end) as m1,sum(case when month = 2 then amount else 0 end) as m2,sum(case when month = 3 then amount else 0 end) as m3,sum(case when month = 4 then amount else 0 end) as m4 from temp group by year;