行转列

多行转多列

数据表 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. codeforces 1B 模拟

    题目大意: 给出两种行列位置的表示方法,一个是Excel表示法,一个是(R,C)坐标表示.给出一种表示,输出另外一种表示. 基本思路: 模拟,首先判断是哪一种表示法,然后转换成另外一种表示方法: 我做 ...

  2. php-异步上传插件

    http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html

  3. Yii2增、删、改、查

    $order_model = OrderHeader::find()->where(['user_id'=>$user_id, 'order_type'=>'1'])->and ...

  4. 51nod-1204 并查集

    你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串(例如其中的第3到第5个数字)问他,该子串中包含了奇数个还是偶数个1,他会回答你的问题,然后你可以继续提问......你怀疑朋友的答案 ...

  5. 关于ACL访问控制的一些问题:AntiACL

    @echo off title AntiACL Made By gwsbhqt color 0a reg query "HKU\S-1-5-19" >nul 2>nul ...

  6. python Pool并行执行

    # -*- coding: utf-8 -*- import time from multiprocessing import Pool def run(fn): #fn: 函数参数是数据列表的一个元 ...

  7. Pandas异常值处理

    import pandas as pd #生成异常数据 df=pd.DataFrame({'col1':[1,120,3,5,2,12,13], 'col2':[12,17,31,53,22,32,4 ...

  8. CSS 案例学习

    1.样式 display:inline-block;可改变a标签,合其可定义宽高 2.a:hover表示鼠标经过 3.background:url(110.png) bottom 表示:给链接一个图片 ...

  9. Python中两大神器&exec() &eval()

    一.神器1 -- 内置函数eval eval是python中的内置函数,它的作用是将字符串变为所对应的表达式,也相当于一个功能代码加双引号变为字符串,而eval又将字符串转为相应的功能,它在使用过程中 ...

  10. zabbix--监控的组件和进程介绍

    上图是zabbix的架构,zabbix proxy(代理),可以减小IO并发. zabbix web GUI是用php写的画图工具,从数据库抓取数据. zabbix database zabbix获取 ...