博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Impala中多列转为一行
阅读量:5041 次
发布时间:2019-06-12

本文共 3222 字,大约阅读时间需要 10 分钟。

之前有一位朋友咨询我,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

转载于:https://www.cnblogs.com/jhcelue/p/6784529.html

你可能感兴趣的文章
【47.76%】【Round #380B】Spotlights
查看>>
Git(使用码云)
查看>>
分享Java web 开发必游之路
查看>>
IIS初始化(预加载),解决第一次访问慢,程序池被回收问题(转载)
查看>>
Bean的Scope
查看>>
【BZOJ】3142: [Hnoi2013]数列
查看>>
http初探
查看>>
elasticsearch的安装
查看>>
__next__()
查看>>
爬取:中国大学排名
查看>>
聊天室(C++客户端+Pyhton服务器)_1.框架搭设
查看>>
UpdatePanel 内控件 更新“外的”控件【转】
查看>>
mybatis中>=和<=的实现方式
查看>>
Python面向对象03/继承
查看>>
java序列化和反序列化
查看>>
绝对定位
查看>>
flink源码编译(windows环境)
查看>>
dpkg 删除 百度网盘 程序
查看>>
服务器nginx安装
查看>>
std::nothrow
查看>>