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
相关推荐
zhixingheyi_tian5 小时前
Hadoop 之 Uber 模式
大数据·hadoop·eclipse
Macbethad5 小时前
WPF 工业设备管理程序技术方案
java·大数据·hadoop
酸奶不吃鱼。6 小时前
hive中的函数
数据仓库·hive·hadoop
clownAdam6 小时前
Hive数仓分层架构必要性分析
hive·hadoop·架构
大叔_爱编程1 天前
基于大数据的短视频用户兴趣分析-hive+django+spider
大数据·hive·django·毕业设计·源码·课程设计·spider
张人玉1 天前
大数据hadoop系列——在ubuntu上安装HBase 伪分布式
大数据·hadoop·分布式·hbase
笨蛋少年派1 天前
数据仓库建设知识扫盲
数据仓库
张人玉1 天前
大数据Hadoop系列——在ubuntu上安装pig数据库
大数据·hadoop·ubuntu·pig
李慕婉学姐1 天前
【开题答辩过程】以《基于Hadoop的医生相关数据分析与可视化及医生推荐系统》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
大数据·hadoop·数据分析
张人玉1 天前
大数据hadoop系列——在ubuntu上安装hadoop完分布式
大数据·hadoop·分布式