之前有一位朋友咨询我,Impala中怎样实现将多列转为一行,事实上Impala中自带函数能够实现,不用自己定义函数。
以下我開始演示:
-bash-4.1$ impala-shell
Starting Impala Shell without Kerberos authentication
Connected to cdha:21000
Server version: impalad version 1.4.2-cdh5 RELEASE (build eac952d4ff674663ec3834778c2b981b252aec78)
Welcome to the Impala shell. Press TAB twice to see a list of available commands.
Copyright (c) 2012 Cloudera, Inc. All rights reserved.
(Shell build version: Impala Shell v1.4.2-cdh5 (eac952d) built on Tue Sep 16 19:15:40 PDT 2014)
[cdha:21000] > create table student(name string,subject string,score decimal(4,1)); -------------创建演示表:student(脱离学校四年了。还不忘自己是学生^_^)
Query: create table student(name string,subject string,score decimal(4,1))
Returned 0 row(s) in 1.97s
[cdha:21000] > insert into student values(‘xiaoming‘,‘math‘,89.5); ----------------插入演示数据 (小明和花花的学习成绩,他们感情比較好)
Query: insert into student values(‘xiaoming‘,‘math‘,89.5)
Inserted 1 rows in 2.56s
[cdha:21000] > insert into student values(‘xiaoming‘,‘english‘,92);
Query: insert into student values(‘xiaoming‘,‘english‘,92)
Inserted 1 rows in 0.39s
[cdha:21000] > insert into student values(‘xiaoming‘,‘chinese‘,98);
Query: insert into student values(‘xiaoming‘,‘chinese‘,98)
Inserted 1 rows in 0.42s
[cdha:21000] > insert into student values(‘huahua‘,‘chinese‘,80);
Query: insert into student values(‘huahua‘,‘chinese‘,80)
Inserted 1 rows in 0.40s
[cdha:21000] > insert into student values(‘huahua‘,‘math‘,89.5);
Query: insert into student values(‘huahua‘,‘math‘,89.5)
Inserted 1 rows in 0.29s
[cdha:21000] > select * from student; ----------每次考完试,老师报分数时都非常紧张
Query: select * from student
+----------+---------+-------+
| name | subject | score |
+----------+---------+-------+
| xiaoming | english | 92.0 |
| huahua | chinese | 80.0 |
| xiaoming | chinese | 98.0 |
| huahua | math | 89.5 |
| xiaoming | math | 89.5 |
+----------+---------+-------+
Returned 5 row(s) in 0.23s
[cdha:21000] > select name,group_concat(subject,‘,‘) from student group by name; ------------小试牛刀,看到了吧,多列拼接到一起了
Query: select name,group_concat(subject,‘,‘) from student group by name
+----------+----------------------------+
| name | group_concat(subject, ‘,‘) |
+----------+----------------------------+
| xiaoming | english,chinese,math |
| huahua | chinese,math |
+----------+----------------------------+
Returned 2 row(s) in 0.38s
------------以下演示有价值的演示样例,显示高手之招,呵呵
------------这样就能够在一行上面,看到小明和花花的各科成绩了
[cdha:21000] > select name,group_concat(concat_ws(‘=‘,subject,cast(score as string)),‘,‘) from student group by name;
Query: select name,group_concat(concat_ws(‘=‘,subject,cast(score as string)),‘,‘) from student group by name
+----------+-------------------------------------------------------------------+
| name | group_concat(concat_ws(‘=‘, subject, cast(score as string)), ‘,‘) |
+----------+-------------------------------------------------------------------+
| xiaoming | english=92.0,chinese=98.0,math=89.5 |
| huahua | chinese=80.0,math=89.5 |
+----------+-------------------------------------------------------------------+
Returned 2 row(s) in 0.39s
[cdha:21000] >
Hive的行列转换请查看: http://blog.csdn.net/jiangshouzhuang/article/details/46810529