hive 行转列

行转列的常规做法是,group by+sum(if())【或count(if())】

建表:

复制代码
CREATE TABLE table2 (
    year INT,
    month INT,
    amount DOUBLE
);

INSERT INTO table2 (year, month, amount) VALUES
    (1991, 2, 1.2),
    (1991, 3, 1.3),
    (1991, 4, 1.4),
    (1992, 1, 2.1),
    (1992, 2, 2.2),
    (1992, 3, 2.3),
    (1992, 4, 2.4);

查询:

首先建立子查询

复制代码
select *,
    if(month=1,amount,0) as a,
    if(month=2,amount,0) as b,
    if(month=3,amount,0) as c,
    if(month=4,amount,0) as d
from table2

然后分组求和一下:

复制代码
select year,sum(a) as m1,
     sum(b)as m2,
     sum(c) as m3,
     sum(d) as m4
from
(
select *,
    if(month=1,amount,0) as a,
    if(month=2,amount,0) as b,
    if(month=3,amount,0) as c,
    if(month=4,amount,0) as d
from table2
 )t1
 group by year

****2.腾讯游戏

首先建表

复制代码
CREATE TABLE table3 (
    ddate varchar(20),
    shengfu varchar(20)
);
insert INTO table3  values ('2015-05-09', "胜"),
('2015-05-09', "胜"),
('2015-05-09', "负"),
('2015-05-09', "负"),
('2015-05-10', "胜"),
('2015-05-10', "负"),
('2015-05-10', "负");

查询

复制代码
select ddate,
SUM(case when shengfu = '胜' then 1 else 0 end) `胜`,
SUM(case when shengfu = '负' then 1 else 0 end) `负`
from table3
group by ddate
相关推荐
Theodore_10221 小时前
大数据(1) 大数据概述
大数据·hadoop·数据分析·spark·hbase
IvanCodes3 小时前
六、Sqoop 导出
大数据·hadoop·sqoop
workflower4 小时前
以光量子为例,详解量子获取方式
数据仓库·人工智能·软件工程·需求分析·量子计算·软件需求
weixin_472339465 小时前
Doris查询Hive数据:实现高效跨数据源分析的实践指南
数据仓库·hive·hadoop
火龙谷6 小时前
【hadoop】相关集群开启命令
大数据·hadoop·分布式
神奇侠202421 小时前
Hive SQL常见操作
hive·hadoop·sql
SelectDB技术团队1 天前
从 ClickHouse、Druid、Kylin 到 Doris:网易云音乐 PB 级实时分析平台降本增效
大数据·数据仓库·clickhouse·kylin·实时分析
itachi-uchiha1 天前
Docker部署Hive大数据组件
大数据·hive·docker
viperrrrrrrrrr71 天前
大数据学习(131)-Hive数据分析函数总结
大数据·hive·学习
Leo.yuan1 天前
API是什么意思?如何实现开放API?
大数据·运维·数据仓库·人工智能·信息可视化