hive的行列互转
行转列
多行转多列
数据表 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的行列互转的更多相关文章
- 关于SQLServer 中行列互转的实例说明
这几天在做一个招标系统中审批模块,其中关于报价信息这块,用到了pivot和unpivot来实现数据的行列互转,下面简单介绍一下,实际案例,便于回忆和记录相关的条件下使用的情况.pivot 与 unpi ...
- Hive中行列转换
1.演示多列转为单行 数据文件及内容: student.txt xiaoming|english|92.0 xiaoming|chinese|98.0 xiaoming|math|89.5 huahu ...
- sqlservcer行列互转
普通行列转换 行转列 假设有张学生成绩表(tb)如下:Name Subject Result张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94*/---- ...
- sql server pivot/unpivot 行列互转
有时候会碰到行转列的需求(也就是将列的值作为列名称),通常我都是用 CASE END + 聚合函数来实现的. 如下: declare @t table (StudentName nvarchar(20 ...
- SQL 表 和字符串 互转 (行列互转)
-- 表转字符串 )) ,,'') --字符串转表 ),)) ,) )) AS BEGIN DECLARE @StartIndex INT --开始查找的位置 DECLARE @FindIndex I ...
- sql server 行列互转
1 列转行 测试脚本 ),课程 ),分数 int) ) ) ) ) ) ) go 转化脚本 select 姓名 , end) 语文, end) 数学 , end) 物理 from tb group b ...
- hive的行列转换
行转列(把多个行合并) 比如把: id tag 1 12 1 23 2 67 2 78 2 76 行转列之后: id tag 1 12,23 2 67,78,76 使用函数为:concat_w ...
- hive sql 行列转换
-- 对一张大表的每一行,后面加多种label值 -- 其实就是笛卡尔积,举例 -- SELECT * FROM dev.dev_jiadian_user_yuge_temp -- CROSS JOI ...
- sql 行列互转
1.行转列 现有数据: 期望数据: 1.1建表建数据 IF OBJECT_ID('temp_20170701','u') IS NOT NULL DROP TABLE temp_20170701 CR ...
随机推荐
- 查看github热门项目
访问 Trending 或者可以通过菜单 Explore -> 选择 "Trending" -- 默认是查看今天在github社区活跃的仓库 在 github 搜索框输入 s ...
- BD贴吧图片爬虫
#encoding:utf-8 import urllib import urllib.request from lxml import etree class Spider(object): def ...
- API equals方法 toString方法
API API: Application(应用) Programming(程序) Interface(接口) 不需要关心这些类是如何实现的,只需要学习这些类如何使用即可. equals方法 1.在 ...
- Oracle系列教程之一软件安装与卸载
1.安装软件 软件下载地址:https://www.oracle.com/technetwork/cn/database/enterprise-edition/downloads/index.html ...
- JMeter目录结构
转载自https://www.cnblogs.com/imyalost/p/6959797.html 首先得了解一下这些东西,以后才能快速的找到某些配置文件进行修改(举个例子,改配置只是其中之一) 一 ...
- Robot Framework:变量与运算
设置变量 ...
- vue基础四
1.绑定Html Class(在 v-bind 用于 class 和 style 时, Vue.js 专门增强了它.表达式的结果类型除了字符串之外,还可以是对象或数组) 1.1对象语法 传给v-bin ...
- CSS格式化---属性排序
一.背景 与同事合作开发一个项目,后面修改 CSS 时,发现属性顺序跟我写的不一样 我从事开发前端时,导师是有给我大概指定了一定的书写规范 现在开发时,看到的 CSS 属性排序不一样,看起来有点难受( ...
- SCP-bzoj-1078
项目编号:bzoj-1078 项目等级:Safe 项目描述: 戳这里 特殊收容措施: 斜堆有一个著名的性质:如果一个节点没有左子树,则它一定没有右子树,这也是它“斜堆”名称的由来. 此题通过给出斜堆来 ...
- Eclipse中properties文件,中文只显示Unicode问题(Properties Editor)
我们常常在properties文件中添加中文注释,而properties文件的中文需用unicode表示, 使用eclipse默认的properties文件编辑器查看显示中文为乱码. 即便修改prop ...