zoukankan      html  css  js  c++  java
  • Hive排序函数

    @

    排名函数

    注意排名函数可以跟Over(),但是不能定义window_clause。在计算名次前,需要先排序!

    • RANK: 允许并列,一旦有并列跳号!
    • ROW_NUMBER: 行号! 连续的,每个号之间差1!
    • DENSE_RANK: 允许并列,一旦有并列不跳号!
    • CUME_DIST: 从排序后的第一行到当前值之间数据 占整个数据集的百分比!
    • PERCENT_RANK: rank-1/ 总数据量-1
    • NTILE(x): 将数据集均分到X个组中,返回每条记录所在的组号
    select  *,rank() over(order by score) ranknum,
    ROW_NUMBER() over(order by score) rnnum,
    DENSE_RANK() over(order by score) drnum,
    CUME_DIST() over(order by score) cdnum,
    PERCENT_RANK() over(order by score) prnum
    from score
    

    在这里插入图片描述

    练习

    表及字段:score.name | score.subject | score.score

    1. 按照科目进行排名
    select *,rank() over(partition by subject order by score desc)
    from score
    

    在这里插入图片描述
    2. 给每个学生的总分进行排名

    select name,sumscore,rank()  over( order by sumscore desc)
    from
    (select name,sum(score) sumscore
    from  score
    group by  name) tmp
    

    在这里插入图片描述
    3. 求每个学生的成绩明细及给每个学生的总分和总分排名

    select *,DENSE_RANK() over(order by tmp.sumscore desc)
    from
    (select *,sum(score) over(partition by name)  sumscore
    from score) tmp
    

    在这里插入图片描述
    4. 只查询每个科目的成绩的前2名

    select *
    from
    (select *,rank() over(partition by subject order by score desc) rn
    from score) tmp
    where rn<=2
    

    在这里插入图片描述
    5. 查询学生成绩明细,并显示当前科目最高分

    select *,max(score) over(partition by subject)
    from score
    # 或
    select *,FIRST_VALUE(score) over(partition by subject order by score desc)
    from score
    

    在这里插入图片描述

  • 相关阅读:
    Linux下wget下载软件小技巧以及安装jdk、tomcat与ftp服务器
    C++计算二叉树的节点数和高度
    转:POST 400 Bad Request The request sent by the client was syntactically incorrect
    SSM项目spring配置文件详细步骤(分门别类、灵巧记忆)
    IntelliJ IDEA里找不到javax.servlet的jar包
    51nod 1533 && CF538F
    51nod 1189
    51nod 1225
    51nod 1040
    51nod 1610
  • 原文地址:https://www.cnblogs.com/sunbr/p/13778948.html
Copyright © 2011-2022 走看看