行转列

多行转多列

数据表 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. Java文件拷贝方式

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11444284.html 利用java.io类库,直接为源文件构建一个FileInputStream读取 ...

  2. WPF ListBox 横向排列

    WPF ListBox 横向排列   如果只是单纯的让ListBox可以横向配列,这样很简单,只需要更改ListBox的ItemsPanel模板就可以,例如: <ListBox><L ...

  3. NX二次开发-使用MFC对话框不能用UF_UI_select等函数解决方法

    VC/MFC调用UG Dialog要进入加锁状态 加锁 UF_UI_lock_ug_access ( UF_UI_FROM_CUSTOM ); 此处为UF_UI_select的函数 解锁 UF_UI_ ...

  4. 调试口:JTAG与SW-Debug Port

  5. LightOJ 1203 Guarding Bananas (凸包最小顶角)

    题目链接:LightOJ 1203 Problem Description Once there was a lazy monkey in a forest. But he loved banana ...

  6. vue-lic工具搭建vue-webpack项目

    1.安装node环境(安装node时候会自动安装npm) 参考官网:https://nodejs.org/en/download/ 2.安装vue的脚手架工具vue-cli // 全局安装 npm i ...

  7. PHP对象在内存中的分配(转载)

    http://www.cnblogs.com/hongfei/archive/2012/06/12/2547120.html 对像在PHP 里面和整型.浮点型一样,也是一种数据类,都是存储不同类型数据 ...

  8. NIO 源码分析(01) NIO 最简用法

    目录 一.服务端 二.客户端 NIO 源码分析(01) NIO 最简用法 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) J ...

  9. ECMS清除挂马以及后台升级实战(从ecms6.6~ecms7.0)

    当时状况 Windows Server 2008 R2 Enterprise + 帝国CMS6.6 + MySql   server软件: Microsoft-IIS/7.5 操作系统: WINNT ...

  10. SYSTEM_HANDLE_TABLE_ENTRY_INFO

    typedef struct _SYSTEM_HANDLE_TABLE_ENTRY_INFO { USHORT UniqueProcessId; USHORT CreatorBackTraceInde ...