Java教程

SQL面试题-行列变换

本文主要是介绍SQL面试题-行列变换,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

在这里插入图片描述

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;

这篇关于SQL面试题-行列变换的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!