HiveSQL——sum(if()) 条件累加

注:参考文章:

HiveSql面试题10--sum(if)统计问题_hive sum if-CSDN博客文章浏览阅读5.8k次,点赞6次,收藏19次。0 需求分析t_order表结构字段名含义oid订单编号uid用户idotime订单时间(yyyy-MM-dd)oamount订单金额(元)所有在2018年1月下过单并且在2月没有下过单的用户,在3月份的下单情况:目标字段名含义_hive sum ifhttps://blog.csdn.net/godlovedaniel/article/details/108325219

0 需求分析

t_order表结构如图:

要求:t_order表扫描次数不超过2次的前提下,统计所有在2018年1月下过单且在2月份没有下过单的用户,该用户在3月份的下单情况:

1 数据准备

sql 复制代码
CREATE TABLE t_order (
       oid int ,
       uid int ,
       otime string,
       oamount int
 )
ROW format delimited FIELDS TERMINATED BY ",";
load data local inpath "/opt/module/hive_data/t_order.txt" into table t_order;

2 数据分析

完整代码为:

sql 复制代码
with tmp as (
    select
        oid,
        uid,
        otime,
        date_format(otime, 'yyyy-MM') as                                                  dt,
        oamount,
        ---计算rk的目的是为了获取记录中的第一条
        row_number() over (partition by uid,date_format(otime, 'yyyy-MM') order by otime) rk,
        --- 计算cnt的目的是为了获取记录中的最后一条
        count(*) over (partition by uid,date_format(otime, 'yyyy-MM'))                    cnt
    from t_order
    order by uid
)
select
    uid,
    --每个用户一月份的订单数
    sum(if(dt = '2018-01', 1, 0)) as                 m1_count,
    --每个用户二月份的订单数
    sum(if(dt = '2018-02', 1, 0)) as                 m2_count,
    --每个用户三月份的订单数(当月订单金额超过10元的订单个数)
    sum(if(dt = '2018-03' and oamount > 10, 1, 0))   m3_count,
    --当月(3月份)首次下单的金额
    sum(if(dt = '2018-03' and rk = 1, oamount, 0))   m3_first_amount,
    --当月(3月份)末次下单的金额(rk =cnt小技巧)
    sum(if(dt = '2018-03' and rk = cnt, oamount, 0))  m3_last_amount
from tmp
group by uid
--将下单记录转化成下单次数判断
having m1_count >0 and m2_count=0;

最终的输出结果为:

上述代码解析:

**step1:**用date_format函数进行日期格式化,row_number() over() 获得排名rk, count(*)over()获得统计值cnt

sql 复制代码
select
        oid,
        uid,
        otime,
        date_format(otime, 'yyyy-MM') as  dt,
        oamount,
        ---计算rk的目的是为了获取记录中的第一条
        row_number() over (partition by uid,date_format(otime, 'yyyy-MM') order by otime) rk,
        --- 计算cnt的目的是为了获取记录中的最后一条
        count(*) over (partition by uid,date_format(otime, 'yyyy-MM'))  cnt
 from t_order

step2:

  • 获取当月订单金额超过10元的订单个数 :sum(if(条件, 1, 0)) 或者 sum( case when 条件 then 1 else 0 end );
  • 获取当月首次下单金额:rk=1
  • 获取当月末次下单金额:rk=cnt (每个分组的记录数cnt 同时也等于分组内,最后一条记录数的排序值rk)
sql 复制代码
with tmp as (
    select
        oid,
        uid,
        otime,
        date_format(otime, 'yyyy-MM') as                                                  dt,
        oamount,
        ---计算rk的目的是为了获取记录中的第一条
        row_number() over (partition by uid,date_format(otime, 'yyyy-MM') order by otime) rk,
        --- 计算cnt的目的是为了获取记录中的最后一条
        count(*) over (partition by uid,date_format(otime, 'yyyy-MM'))                    cnt
    from t_order
    order by uid
)
select
    uid,
    --每个用户一月份的订单数
    sum(if(dt = '2018-01', 1, 0)) as                 m1_count,
    --每个用户二月份的订单数
    sum(if(dt = '2018-02', 1, 0)) as                 m2_count,
    --每个用户三月份的订单数(当月订单金额超过10元的订单个数)
    sum(if(dt = '2018-03' and oamount > 10, 1, 0))   m3_count,
    --当月(3月份)首次下单的金额
    sum(if(dt = '2018-03' and rk = 1, oamount, 0))   m3_first_amount,
    --当月(3月份)末次下单的金额(rk =cnt小技巧)
    sum(if(dt = '2018-03' and rk = cnt, oamount, 0))  m3_last_amount
from tmp
group by uid
having m1_count >0 and m2_count=0;

3 小结

本案例用到的知识点:

  • sum(if()) 有条件累加;
  • row_number() over(partition by ....order by ..) 排序,求分组topN
  • count(*) over(partition by ...) 分组统计记录数。每组的记录数同时也是最后一条记录的排序值。
  • 将下单记录转化成下单次数判断 m1_count >0 and m2_count=0;
相关推荐
Aloudata9 小时前
制造企业做库存周转分析:沿用 ERP 报表,还是用语义层统一跨工厂口径?
大数据·数据分析·制造·语义层
顧棟9 小时前
Elasticsearch LifecycleComponent 组件生命周期设计分析
大数据·elasticsearch
qyr678910 小时前
全球汽车和航空航天用磁铁材料市场竞争格局变化趋势
大数据·人工智能·物联网·自动化·汽车和航空航天用磁铁材料
大大大大晴天13 小时前
范式建模、维度建模与宽表建模:企业数仓该如何取舍
大数据
货拉拉技术16 小时前
货拉拉大数据智能运维AI Agent探索实践
大数据·人工智能
TDengine (老段)17 小时前
TDengine 读缓存与 LAST 查询完全指南
大数据·数据库·物联网·缓存·时序数据库·tdengine·涛思数据
字节跳动数据平台17 小时前
知识引擎 VeKnow 文档解析实践:文字提取之后,如何保留结构与依据
大数据
武子康20 小时前
100个AI玩家,不等于100位真实玩家
大数据·人工智能·claude
linweidong20 小时前
从零搭建 Iceberg 数据湖:存储、查询与运维实战
大数据·spark·iceberg·实时数仓·数据湖·湖仓一体·大数据面试
存储架构师-iVY21 小时前
从端侧192GB到云端PCIe 5.0:拆解AI时代的存储架构演进
大数据·人工智能·ai存储·lpcamm2·socamm2·pcie 5.0·统一内存架构