Impala中多列转为一行

原创 2015年07月08日 22:44:40

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Hive中行列转换

1、演示多列转为单行 数据文件及内容: student.txt xiaoming|english|92.0 xiaoming|chinese|98.0 xiaoming|math|89.5 huahu...

SQL 面试经典问题 行列互相转化

SQL 面试经典问题 行列互相转化 1.行转列 select 姓名 as 姓名 , max(case 课程 when '语文' then 分数 else 0 end) 语文, max...

Impala 教程

Impala 教程 Set Up Some Basic .csv Tables Impala 表指向已存的数据文件 查看 Impala 表结构 查询 Impala 表 数据加载与查询的例子 ...
  • opensure
  • opensure
  • 2015年05月19日 21:54
  • 31292

impala,hive行转列

Hive For Hive, I use collect_set() + concat_ws() from https://cwiki.apache.org/confluence/display...

SQLServer多行多列数据转为一行

SQLServer多行多列数据转为一行

SQL Server获取一行中多列的最大值三种方法

有些时候我们需要从一个表的多个列中获取一个最大值(这些列的数据类型相同,并且是每一行记录都要获取一个最大值)。下面讲了三种方法,我们一起看下。 首先我们创建一个表,并在表中插入需要用到的记录,如下...

SQL将一行多列数据合并成 一列

由于最近在做项目的时候,要对数据库中一行很多列个数据进行求和,便在网上查了一下,首先要用到动态SQL语句,动态sql感觉就是类似于拼接sql字符串 动态语句基本语法 1 :普通SQL语句可以用ex...

hdu1045Fire Net (一行变多行,一列变多列,最小顶点覆盖)

Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm...

NiceLabel一行多列的条码标签如何打印?

很多用户在NiceLabel打印条码标签时,可能会根据标签纸的实际大小,需要在NiceLabel设置一行多列的标签...

HNU13028Attacking rooks (二分匹配,一行变多行,一列变多列)

Attacking rooks Time Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:65536KB Tot...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Impala中多列转为一行
举报原因:
原因补充:

(最多只允许输入30个字)