美文网首页
Hive基础增强-(窗口函数)

Hive基础增强-(窗口函数)

作者: bigdata张凯翔 | 来源:发表于2020-06-26 23:35 被阅读0次

    一.原始数据

            jack,2017-01-01,10
            tony,2017-01-02,15
            jack,2017-02-03,23
            tony,2017-01-04,29
            jack,2017-01-05,46
            jack,2017-04-06,42  -
            tony,2017-01-07,50
            jack,2017-01-08,55
            mart,2017-04-08,62  -
            mart,2017-04-09,68  -
            neil,2017-05-10,12
            mart,2017-04-11,75  -
            neil,2017-06-12,80
            mart,2017-04-13,94  -
    

    执行如下函数后分别得到不同的结果,以此来理解开窗函数的使用方法

    select name,count(*)
    from business
    where month(orderdate)='4'
    group by name;
    
    +-------+------+--+
    | name  | _c1  |
    +-------+------+--+
    | jack  | 1    |
    | mart  | 4    |
    +-------+------+--+
    
    select name,count(*) over()
    from business
    where month(orderdate)='4'
    group by name;
    
    +-------+-----------------+--+
    | name  | count_window_0  |
    +-------+-----------------+--+
    | jack  | 2               |
    | mart  | 2               |
    +-------+-----------------+--+
    
    select name,count(*) over()
    from business
    where month(orderdate)='4';
    +-------+-----------------+--+
    | name  | count_window_0  |
    +-------+-----------------+--+
    | jack  | 5               |
    | mart  | 5               |
    | mart  | 5               |
    | mart  | 5               |
    | mart  | 5               |
    +-------+-----------------+--+
    
    select name,count(*) over(partition by name)
    from business
    where month(orderdate)='4'
    group by name;
    
    +-------+-----------------+--+
    | name  | count_window_0  |
    +-------+-----------------+--+
    | jack  | 1               |
    | mart  | 1               |
    +-------+-----------------+--+
    
    
    select name,count(*) over(partition by name)
    from business
    where month(orderdate)='4'
    
    +-------+-----------------+--+
    | name  | count_window_0  |
    +-------+-----------------+--+
    | jack  | 1               |
    | mart  | 4               |
    | mart  | 4               |
    | mart  | 4               |
    | mart  | 4               |
    +-------+-----------------+--+
    

    二.原始数据

    相关函数说明
    OVER():指定分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变而变化。
    CURRENT ROW:当前行
    n PRECEDING:往前n行数据
    n FOLLOWING:往后n行数据
    UNBOUNDED:起点,UNBOUNDED PRECEDING 表示从前面的起点, UNBOUNDED FOLLOWING表示到后面的终点
    LAG(col,n,default_val):往前第n行数据
    LEAD(col,n, default_val):往后第n行数据
    NTILE(n):把有序分区中的行分发到指定数据的组中,各个组有编号,编号从1开始,对于每一行,NTILE返回此行所属的组的编号。注意:n必须为int类型

                name|orderdate|cost
                jack,2017-01-01,10
                tony,2017-01-02,15
                jack,2017-02-03,23
                tony,2017-01-04,29
                jack,2017-01-05,46
                jack,2017-04-06,42
                tony,2017-01-07,50
                jack,2017-01-08,55
                mart,2017-04-08,62
                mart,2017-04-09,68
                neil,2017-05-10,12
                mart,2017-04-11,75
                neil,2017-06-12,80
                mart,2017-04-13,94
    create table business(
    name string,orderdate string,cost int)
    row format delimited fields terminated by ','
    load data local inpath "/opt/module/datas/business.txt"
    into table business;
    
    ##按需求查询
    1.查询在2017年4月购买过的顾客及总人数
    select name,count(*) over()
    from business
    where subString(orderdate,1,7)='2017-04'
    group by name;
    2.查询顾客的购买明细及月购买总额
    select name,sum(cost) over(partition by month(orderdate))
    from business
    3.上述的场景,将每个顾客的cost按照日期进行累加
    select name,orderdate,cost,
    sum(cost) over() as sample1, --将所有行相加
    sum(cost) over(partition by name) as sample2,--按name分组,组内数据相加
    sum(cost) over(partition by name order by orderdate) as sample3,--按name分组,组内数据累加 
    sum(cost) over(partition by name order by orderdate rows between UNBOUNDED PRECEDING and current row) as sample4,--和sample3一样,由起点到当前行的聚合
    sum(cost) over(partition by name order by orderdate rows between 1 PRECEDING AND current row) as sample5,--当前行和前面一行做聚合 
    sum(cost) over(partition by name order by orderdate rows between 1 PRECEDING and 1 FOLLOWING) as simple6,--当前行和前边一行及后面一行 
    sum(cost) over(partition by name order by orderdate rows between current row and UNBOUNDED FOLLOWING) as sample7 --当前行及后面所有行
    from business
    
    rows必须跟在Order by 子句之后,对排序的结果进行限制,使用固定的行数来限制分区中的数据行数量
    4.查看顾客上次的购买时间(lag 往前n行)
    
    select name,orderdate,cost,
    lag(orderdate,1,'1970-01-01') over(partition by name order by orderdate ) as time1,
    lag(orderdate,2) over(partition by name order by orderdate) as time2
    from business;
    
    结果:
    name    orderdate   cost    time1   time2
    jack    2017-01-01  10  1970-01-01  NULL
    jack    2017-01-05  46  2017-01-01  NULL
    jack    2017-01-08  55  2017-01-05  2017-01-01
    jack    2017-02-03  23  2017-01-08  2017-01-05
    jack    2017-04-06  42  2017-02-03  2017-01-08
    mart    2017-04-08  62  1970-01-01  NULL
    mart    2017-04-09  68  2017-04-08  NULL
    mart    2017-04-11  75  2017-04-09  2017-04-08
    mart    2017-04-13  94  2017-04-11  2017-04-09
    neil    2017-05-10  12  1970-01-01  NULL
    neil    2017-06-12  80  2017-05-10  NULL
    tony    2017-01-02  15  1970-01-01  NULL
    tony    2017-01-04  29  2017-01-02  NULL
    tony    2017-01-07  50  2017-01-04  2017-01-02
    
    NTILE(n):把有序分区中的行分发到指定数据的组中,
    各个组有编号,编号从1开始,对于每一行,NTILE返回此行所属的组的编号。
    注意:n必须为int类型。
    
    查询前20%时间的订单信息:
    
        select * from(
          select name,orderdate,cost,ntile(5)
          over(order by orderdate) sorted
          from business
        ) t
        where sorted = 1;
    

    三.Rank

    1.函数说明

    Rank() 排序相同时会重复,总数不会变
    DENSE_RANK() 排序相同时会重复,总数会减少
    ROW_NUMBER() 会根据顺序计算

    数据准备:

    name    subject score
    孙悟空 语文  87
    孙悟空 数学  95
    孙悟空 英语  68
    大海  语文  94
    大海  数学  56
    大海  英语  84
    宋宋  语文  64
    宋宋  数学  86
    宋宋  英语  84
    婷婷  语文  65
    婷婷  数学  85
    婷婷  英语  78
    

    2.创建hive表并导入数据

    create table score(
    name string,
    subject string, 
    score int) 
    row format delimited fields terminated by "\t";
    load data local inpath '/opt/module/datas/score.txt' into table score;
    

    3.按需求查询数据

    select name,subject,score,
    rank() over(partition by subject order by score desc) rp,
    dense_rank() over(partition by subject order by score desc) drp,
    row_number() over(partition by subject order by score desc) rmp
    from score;
    

    结果如下:

    name    subject score   rp      drp     rmp
    孙悟空  数学    95      1       1       1
    宋宋    数学    86      2       2       2
    婷婷    数学    85      3       3       3
    大海    数学    56      4       4       4
    宋宋    英语    84      1       1       1
    大海    英语    84      1       1       2
    婷婷    英语    78      3       2       3
    孙悟空  英语    68      4       3       4
    大海    语文    94      1       1       1
    孙悟空  语文    87      2       2       2
    婷婷    语文    65      3       3       3
    宋宋    语文    64      4       4       4
    

    转载自

    https://www.jianshu.com/p/fd86a9743045

    相关文章

      网友评论

          本文标题:Hive基础增强-(窗口函数)

          本文链接:https://www.haomeiwen.com/subject/jgbufktx.html