行转列

多行转多列

数据表 row2col
col1   col2    col3
a c 1
a d 2
a e 3
b c 4
b d 5
b e 6
现在要将其转化为:
col1   c      d      e
a 1 2 3
b 4 5 6
此时需要使用到max(case … when … then … else 0 end),仅限于转化的字段为数值类型且为正值的情况
创建表:
create table row2col(col1 string,col2 string,col3 int)
row format delimited
fields terminated by ',';
加载数据:
load data local inpath '/root/hivedata/row2col.txt' into table row2col;
a,c,1
a,d,2
a,e,3
b,c,4
b,d,5
b,e,6
select col1,
max(case col2 when 'c' then col3 else 0 end) as c,
max(case col2 when 'd' then col3 else 0 end) as d,
max(case col2 when 'e' then col3 else 0 end) as e
from row2col
group by col1;

多行转单列(重要)

数据表 row2col_1:
col1    col2    col3
a b 1
a b 2
a b 3
c d 4
c d 5
c d 6
将其转化为:
col1    col2    col3
a b 1,2,3
c d 4,5,6
此时需要两个内置的函数:
a)concat_ws(参数1,参数2),用于进行字符的拼接 
    参数1—指定分隔符 
    参数2—拼接的内容 
b)collect_set(col3),它的主要作用是将某字段的值进行去重汇总,产生array类型字段
   如果不想去重可用collect_list()
创建表:
create table row2col_1(col1 string,col2 string,col3 int)
row format delimited
fields terminated by ',';
加载数据:
load data local inpath '/root/hivedata/row2col_1.txt' into table row2col_1;
a,b,1
a,b,2
a,b,3
c,d,4
c,d,5
c,d,6
select col1, col2, concat_ws('&', collect_set(cast(col3 as string))) as col3
from row2col_1
group by col1, col2;
 

列转行

多列转多行

数据表 col2row:
col1   c      d      e
a 1 2 3
b 4 5 6
现要将其转化为:
col1   col2    col3
a c 1
a d 2
a e 3
b c 4
b d 5
b e 6
这里需要使用union进行拼接。
union 可以结合多个select语句 返回共同的结果集
保证每个select语句返回的数据类型个数是一致的。
创建表:
create table col2row(col1 string,c int,d int,e int)
row format delimited
fields terminated by ',';
加载数据:
load data local inpath '/root/hivedata/col2row.txt' into table col2row;
a,1,2,3
b,4,5,6
 
select col1, 'c' as col2, c as col3 from col2row
UNION
select col1, 'd' as col2, d as col3 from col2row
UNION
select col1, 'e' as col2, e as col3 from col2row
order by col1, col2;
  • 单列转多行(重要)

数据表 col2row_2:

col1 col2 col3
a b 1,2,3
c d 4,5,6

现要将其转化为:

col1 col2 col3
a b 1
a b 2
a b 3
c d 4
c d 5
c d 6

这里需要使用UDTF(表生成函数)explode(),该函数接受array类型的参数,其作用恰好与collect_set相反,实现将array类型数据行转列。explode配合lateral view实现将某列数据拆分成多行。

创建表:

create table col2row_2(col1 string,col2 string,col3 string)
row format delimited
fields terminated by '\t';

加载数据:

load data local inpath '/root/hivedata/col2row_2.txt' into table col2row_2;
a b 1,2,3
c d 4,5,6
select col1, col2, lv.col3 as col3
from col2row_2
lateral view explode(split(col3, ',')) lv as col3;

 

hive的行列互转的更多相关文章

  1. 关于SQLServer 中行列互转的实例说明

    这几天在做一个招标系统中审批模块,其中关于报价信息这块,用到了pivot和unpivot来实现数据的行列互转,下面简单介绍一下,实际案例,便于回忆和记录相关的条件下使用的情况.pivot 与 unpi ...

  2. Hive中行列转换

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

  3. sqlservcer行列互转

    普通行列转换 行转列 假设有张学生成绩表(tb)如下:Name Subject Result张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94*/---- ...

  4. sql server pivot/unpivot 行列互转

    有时候会碰到行转列的需求(也就是将列的值作为列名称),通常我都是用 CASE END + 聚合函数来实现的. 如下: declare @t table (StudentName nvarchar(20 ...

  5. SQL 表 和字符串 互转 (行列互转)

    -- 表转字符串 )) ,,'') --字符串转表 ),)) ,) )) AS BEGIN DECLARE @StartIndex INT --开始查找的位置 DECLARE @FindIndex I ...

  6. sql server 行列互转

    1 列转行 测试脚本 ),课程 ),分数 int) ) ) ) ) ) ) go 转化脚本 select 姓名 , end) 语文, end) 数学 , end) 物理 from tb group b ...

  7. hive的行列转换

    行转列(把多个行合并) 比如把: id    tag 1 12 1 23 2 67 2  78 2 76 行转列之后: id tag 1 12,23 2 67,78,76 使用函数为:concat_w ...

  8. hive sql 行列转换

    -- 对一张大表的每一行,后面加多种label值 -- 其实就是笛卡尔积,举例 -- SELECT * FROM dev.dev_jiadian_user_yuge_temp -- CROSS JOI ...

  9. sql 行列互转

    1.行转列 现有数据: 期望数据: 1.1建表建数据 IF OBJECT_ID('temp_20170701','u') IS NOT NULL DROP TABLE temp_20170701 CR ...

随机推荐

  1. vue-resource请求

    man.js引入 import Vue from 'vue' import VueResource from 'vue-resource' import App from './App.vue' Vu ...

  2. mysql查找字段空、不为空的方法总结

    1.不为空 Select * From table_name Where id<>'' Select * From table_name Where id!='' 2.为空 Select ...

  3. spark代码写入hdfs错误

    报错: org.apache.hadoop.security.AccessControlException: Permission denied: user=hgm, access=WRITE 其实就 ...

  4. 关系型数据库---MYSQL---系统学习

    1.概述 1.1 mysql数据库是一种  客户端/服务器体系  的 数据库系统: 服务器部分 在启动运行后没有人机界面,所以终端用户  无法直接使用MySQL: 对MySQL数据库进行访问.操作  ...

  5. 深入理解Magento – 第二章 – Magento请求分发与控制器

    深入理解Magento 作者:Alan Storm 翻译:Hailong Zhang 第二章 – Magento请求分发与控制器 Model-View-Controller (MVC) ,模型-视图- ...

  6. Vue学习笔记【17】——配置本地数据库和数据接口API

    先解压安装 PHPStudy; 解压安装 Navicat 这个数据库可视化工具,并激活: 打开 Navicat 工具,新建空白数据库,名为 dtcmsdb4; 双击新建的数据库,连接上这个空白数据库, ...

  7. FFT快速傅里叶模板

    FFT快速傅里叶模板…… /* use way: assign : h(x) = f(x) * g(x) f(x):len1 g(x):len2 1. len = 1; while(len < ...

  8. Linux环境下安装PHP的gd库

    当前使用的安装包版本: freetype-2.4.0.tar.bz2 jpegsrc.v9.tar.gz libpng-1.6.28.tar.gz 1.安装freetype tar jxvf free ...

  9. LInux多线程编程----线程属性pthread_attr_t

    1.每个POSIX线程有一个相连的属性对象来表示属性.线程属性对象的类型是pthread_attr_t,pthread_attr_t 在文件/usr/include/bits/pthreadtypes ...

  10. Java-Class-@I:java.annotation.Resource

    ylbtech-Java-Class-@I:java.annotation.Resource 1.返回顶部   2.返回顶部 1.1. import javax.annotation.Resource ...