Hive详解(4)

Hive

窗口函数

分析函数

  1. 聚合函数,例如sumavgmaxmin

  2. 移位函数

    1. lag(colName, n):以当前行为基础,来处理第前n行的数据

    2. lead(colName, n):以当前行为基础,来处理第后n行的数据

    3. ntile(n):要求数据必须有序,将有序的数据依次放入n个桶中,保证每个桶中的数据几乎一致,相差最多不超过1个

  3. 排序函数

    1. row_number:数据排序之后,按顺序给数据进行编号,即使数据相同,也是给定不同的编号

    2. rank:数据排序之后,按顺序给数据进行编号,如果数据相同,则给定相同的序号,会产生空位

    3. dense_rank:数据排序之后,按顺序给数据进行编号,如果数据相同,则给定相同的序号,但是不会产生空位

移位函数案例

  1. 需求二:查询每一位顾客的消费明细以及上一次的消费时间

    复制代码
    select *,
           lag(order_date, 1) over (partition by name order by order_date) as last_order_date
    from orders;
  2. 需求三:查询最早进店消费的前20%的顾客信息

    复制代码
    select * from (
        select *,
               ntile(5) over (order by order_date) as n
        from orders
    ) t1 where n = 1;

排序函数案例

  1. 原始数据

    复制代码
    Bob Chinese 85
    Alex Chinese 76
    Bill Chinese 78
    David Chinese 92
    Jack Chinese 69
    Lucy Chinese 74
    LiLy Chinese 78
    Bob Maths 91
    Alex Maths 82
    Bill Maths 69
    David Maths 60
    Jack Maths 69
    Lucy Maths 71
    LiLy Maths 82
    Bob English 60
    Alex English 62
    Bill English 85
    David English 85
    Jack English 69
    Lucy English 78
    LiLy English 93
  2. 案例

    复制代码
    -- 建表
    create table scores (
        name    string,
        subject string,
        score   int
    ) row format delimited fields terminated by ' ';
    -- 加载数据
    load data local inpath '/opt/hive_data/scores' into table scores;
    -- 查询数据
    select *
    from scores tablesample (5 rows);
    -- 按科目对成绩进行降序排序
    select *,
           row_number() over (partition by subject order by score desc) as rn,
           rank() over (partition by subject order by score desc)       as ra,
           dense_rank() over (partition by subject order by score desc) as dr
    from scores;
    -- 获取各科目前三名的信息
    select * from (
        select *, rank() over (partition by subject order by score desc) as n from scores
    ) t where n <= 3;
相关推荐
运维行者_1 小时前
OpManager MSP NetFlow Analyzer集成解决方案,应对多客户端网络流量监控挑战
大数据·运维·服务器·网络·数据库·自动化·运维开发
极创信息6 小时前
信创系统认证服务怎么做?从适配到验收全流程指南
java·大数据·运维·tomcat·健康医疗
大大大大晴天️7 小时前
Flink技术实践-Flink SQL 开发中的隐蔽陷阱
大数据·sql·flink
Gofarlic_OMS8 小时前
Windchill的license合规使用报告自动化生成与审计追踪系统
大数据·运维·人工智能·云原生·自动化·云计算
xcbrand8 小时前
文旅行业品牌策划公司找哪家
大数据·运维·人工智能·python
隐于花海,等待花开8 小时前
FIND_IN_SET 与 LIKE 函数:使用场景及性能对比
hive
zxsz_com_cn8 小时前
设备预测性维护故障预警规则与原理解析
大数据·人工智能
hughnz10 小时前
AI和自动化让油田钻工慢慢消失
大数据·人工智能
juniperhan10 小时前
Flink 系列第8篇:Flink Checkpoint 全解析(原理+流程+配置+优化)
大数据·分布式·flink
GIS数据转换器10 小时前
延凡低成本低空无人机AI巡检方案
大数据·人工智能·信息可视化·数据挖掘·无人机