• Hive 中常用的函数以及数据类型


    数据类型

    1.基本数据类型:

    数据类型大小范围示例
    TINYINT1byte-128 ~ 127100Y
    SMALLINT2byte-32768 ~ 32767100S
    INT4byte-2^32~ 2^32-1100
    BIGINT8byte-2^64~ 2^64-1100L
    FLOAT4byte单精度浮点数5.21
    DOUBLE8byte双精度浮点数5.21
    DECIMAL-高精度浮点数DECIMAL(9,8)
    BOOLEAN-布尔型true/false
    BINARY-字节数组-

    2.字符串类型:

    数据类型长度示例
    STRING-'abc'
    VARCHAR1-65535'abc'
    CHAR1-255'abc'

    对于VARCHAR创建时需指定长度,如果插入的字符串超过了指定的长度,则会被截断,尾部的空格也会作为字符串的一部分,影响字符串的比较。
    对于CHAR类型来说,它是固定长度的,如果插入的字符串长度不如指定的长度,则会用空格补齐。但是尾部的空格不影响字符串的比较。

    3.日期与时间戳类型:(格式很重要,格式不对加载数据为空值)

    数据类型格式示例
    DATEyyyy-MM-dd2020-07-04
    TIMESTAMPSyyyy-MM-dd HH:mm:ss.fffffffff2020-07-04 12:36:25.111

    4.集合类型:

    ARRAYARRAY 类型是由一系列相同数据类型的元素组成,这些元素可以通过下标来访问。 比如有一个 ARRAY 类型的变量 fruits,它是由['apple','orange','mango']组成,可以由下标fruits[1]来访问元素orange。hive中经过split拆分后为ARRAY类型;
    MAP:MAP 包含 key->value 键值对,可以通过 key 来访问元素。比如变量userlist是一个 map类型:username:password,需要通过userlist['username']来得到这个用户对应的 password。
    STRUCT:STRUCT 可以包含不同数据类型的元素。这些元素可以通过点语法的方式来得到所需要的元素,比如 user 是一个 STRUCT 类型:15,北京。可以通过 user.address 得到这个用户的地址。

    存储格式

    Hive会为每个创建的数据库在HDFS上创建一个目录,该数据库的表会以子目录形式存储,表中的数据会以表目录下的文件形式存储。对于default数据库,默认的缺省数据库没有自己的目录,default数据库下的表默认存放在/user/hive/warehouse目录下。

      textfile为默认格式,存储方式为行存储。数据不做压缩,磁盘开销大,数据解析开销大。 
    SequenceFile是Hadoop API提供的一种二进制文件支持,具有使用方便、可分割、可压缩的特点。 

    SequenceFile支持三种压缩选择:NONE, RECORD, BLOCK。 Record压缩率低,一般建议使用BLOCK压缩。 

    RCFile    一种行列存储相结合的存储方式。 
    ORCFile    数据按照行分块,每个块按照列存储,其中每个块都存储有一个索引。hive给出的新格式,属于RCFILE的升级版,性能有大幅度提升,而且数据可以压缩存储,压缩快 快速列存取。 

    Parquet    Parquet也是一种行式存储,同时具有很好的压缩性能;同时可以减少大量的表扫描和反序列化的时间。

    数据格式

    当数据存储在文本文件中,必须按照一定格式区别行和列,并且在Hive中指明这些区分符。Hive默认使用了几个平时很少出现的字符,这些字符一般不会作为内容出现在记录中。
    \n    对于文本文件来说,每行是一条记录,所以\n 来分割记录
    ^A (Ctrl+A)    分割字段,也可以用\001 来表示
    ^B (Ctrl+B)    用于分割 Arrary 或者 Struct 中的元素,或者用于 map 中键值之间的分割,也可以用\002 分割。
    ^C    用于 map 中键和值自己分割,也可以用\003 表示。

    常见函数 内置函数

    1.数值函数​​​​​​​​​​​​​​​​​​​​​

    指定精度的取整函数 round(a,b) 返回值: DOUBLE b指定精度

    ceil 向上取整函数 floor 向下取整函数

    select round(12.34567,2),ceil(12.3456),`floor`(12.3456) ;

    随机数函数 round 返回0~1的随机数

    1. --获取1-100
    2. select ceil(rand()*100);

    取余函数 mod

    1. --取余
    2. select mod(10,3),10%3;

    幂函数 pow 开方函数 sqrt

    1. --计算次幂
    2. select pow(2,3),power(2,3);
    3. --开平方
    4. select sqrt(9);

    2.日期函数

    to_date(string timestamp):返回时间字符串中的日期部分,

    如to_date('1970-01-01 00:00:00')='1970-01-01' 

    current_date:返回当前日期
    current_timestamp:返回当前日期和时间
    year(date):返回日期date的年,类型为int

    month(date):返回日期date的月,类型为int,

    day(date): 返回日期date的天,类型为int,

    hour(date):返回日期date的时,类型为int
    weekofyear(date1):返回日期date1位于该年第几周。

    datediff(date1,date2):返回日期date1与date2相差的天数

    date_add(date1,int1):返回日期date1加上int1的日期

    date_sub(date1,int1):返回日期date1减去int1的日期

    months_between(date1,date2):返回date1与date2相差月份

    add_months(date1,int1):返回date1加上int1个月的日期,int1可为负数

    last_day(date1):返回date1所在月份最后一天

    trunc(date1,string1):返回日期最开始年份或月份。string1可为年(YYYY/YY/YEAR)或月 (MONTH/MON/MM)。

    unix_timestamp():返回当前时间的unix时间戳,可指定日期格式。

    from_unixtime():返回unix时间戳的日期,可指定格式。

    3.条件函数

    if(boolean,t1,t2):若布尔值成立,则返回t1,反正返回t2。

    1. select content,if(count_iphone>7,count_iphone,0) from (
    2. select content, (length(content)-length(regexp_replace(content,'(iphone)|(iPhone)','')))/length('iphone') count_iphone
    3. from dw_weibo where locate('iPhone',content)>0 or locate('iphone',content)>0) h;

    case when boolean then t1 else t2 end:若布尔值成立,则t1,否则t2,可加多重判断 非空查找函数

    1. select content,case when count_iphone>10 then count_iphone end from(
    2. select content, (length(content)-length(regexp_replace(content,'(iphone)|(iPhone)','')))/length('iphone') count_iphone
    3. from dw_weibo where locate('iPhone',content)>0 or locate('iphone',content)>0) k;

    coalesce(v0,v1,v2):返回参数中的第一个非空值,若所有值均为null,则返回null。coalesce(null,1,2)返回1

    4.字符串函数

    length(string1):返回字符串长度
    concat(s1,s2,s3,...):返回拼接string1及string2后的字符串,如果拼接的数据有null则结果为null
    concat_ws(sep,s1,s2,....):返回按指定分隔符拼接的字符串,只能拼接字符串,支持空值拼接
    lower(string1):返回小写字符串,同lcase(string1)upper()/ucase():返回大写字符串
    trim(string1):去字符串左右空格,ltrim(string1):去字符串左空格。rtrim(string1):去字符串右空 repeat(string1,int1):返回重复string1字符串int1次后的字符串
    reverse(string1):返回string1反转后的字符串。

    rpad(string1,len1,pad1):以pad1字符右填充string1字符串,至len1长度。

    split(string1,pat1):以pat1正则分隔字符串string1,返回数组。

    substr(string1,index1,int1):以index位置起截取int1个字符。

    get_json_object:json解析函数

    1. with tmp as (
    2. select '[{"beCommentWeiboId":"","beForwardWeiboId":"","catchTime":"1387158842","commentCount":"1288","content":"秋天要走了嗎?捨不得你耶再留一回回兒吧!@最美和声","createTime":"1382041054","info1":"","info2":"","info3":"","mlevel":"","musicurl":[],"pic_list":["http://ww4.sinaimg.cn/square/687489f8jw1e9ottklbauj20vk0l1whj.jpg","http://ww3.sinaimg.cn/square/687489f8jw1e9ottmdchlj20vk0l1mzx.jpg","http://ww3.sinaimg.cn/square/687489f8jw1e9ottoedd1j20et0m8acf.jpg","http://ww4.sinaimg.cn/square/687489f8jw1e9ottrjaszj20hs0npjvg.jpg","http://ww1.sinaimg.cn/square/687489f8jw1e9ottts4fqj20hs0qp41r.jpg","http://ww1.sinaimg.cn/square/687489f8jw1e9ottwp24xj20hs0q9771.jpg","http://ww4.sinaimg.cn/square/687489f8jw1e9ottyt3d7j20hs0buwg0.jpg","http://ww2.sinaimg.cn/square/687489f8jw1e9otu0urofj20hs0dcdhw.jpg","http://ww3.sinaimg.cn/square/687489f8jw1e9otu9znc8j218g0xc17y.jpg"],"praiseCount":"6860","reportCount":"1303","source":"iPhone客户端","userId":"1752467960","videourl":[],"weiboId":"3634605976485130","weiboUrl":"http://weibo.com/1752467960/Aewg5rd4S"}]
    3. ' str
    4. )
    5. select get_json_object(str,'$.[0].content') ,
    6. split(regexp_replace(get_json_object(str,'$.[0].pic_list'),'\\[|\\]|"',''),',') from tmp;

    parse_url :url解析函数

    1. select parse_url('https://mp.csdn.net/mp_blog/creation/editor/137919192?spm=1011.2124.3001.9778','HOST')
    2. --https://mp.csdn.net

    space:空格字符串函数 返回指定空格个数

    ascii :首字符asc码函数

    find_in_set:集合查找函数

    select find_in_set('aaa','qqqq,rrrr,yyyy,aaa,gggg');

    regexp_extract:正则表达式解析函数

    1. select regexp_extract('100-200','(\\d+)-(\\d+)',2)
    2. --200

    regexp_replace:正则替代函数

    select regexp_replace('100-200','(\\d+)','num')

    5.集合函数

    size:获取map或者arry的个数 size(map) or size(ARRY)

    map_keys:获取map中key的列表

    map_values:获取map中value的列表

    1. select map_keys(scores) from st_u;
    2. -- ["语文","数学"]
    3. select map_values(st_u.scores) from st_u;
    4. -- [90,88]

    arry_contains:判断数组是否包含某元素

    1. select * from goods where array_contains(goods.goods,"衣服");
    2. -- 1,2022-03-04,"[""衣服"",""鞋子"",""电脑""]",102,"{""name"":""zhangsan"",""phone"":"" 12123213""}"
    3. -- 3,2022-03-04,"[""衣服"",""鞋子"",""电脑""]",102,"{""name"":""zhangsan"",""phone"":"" 12123213""}"
    4. -- 5,2022-03-04,"[""衣服"",""鞋子"",""电脑""]",102,"{""name"":""zhangsan"",""phone"":"" 12123213""}"

    sort_arry:数组排序函数

    1. select sort_array(`array`(2,54,23,1,32423))
    2. -- [1,2,23,54,32423]

    6.类型转换函数

    任意之间的数据类型转换:cast

    select concat_ws('-',ename,job,hiredate,cast(comm as string)) from emp;

    7.数据脱敏函数

    mask 将查询数据结果 大写字母变为X 小写字母变为x 数字变为n

    1. select mask('12312ASUSUUchcudhid');
    2. -- nnnnnXXXXXXxxxxxxxx

    mask_frist_n 前n个变换

    1. select mask_first_n('12312ASUSUUchcudhid',7);
    2. -- nnnnnXXUSUUchcudhid

    mask_last_n 后n个脱敏

    1. select mask_last_n('12312ASUSUUchcudhid',7);
    2. -- 12312ASUSUUcxxxxxxx

    mask_show_frist_n :除了前n个 其余脱敏

    1. select mask_show_first_n('12312ASUSUUchcudhid',7);
    2. -- 12312ASXXXXxxxxxxxx

    mask_show_last_n :除了后n个其余脱敏

    1. select mask_show_last_n('12312ASUSUUchcudhid',7);
    2. -- nnnnnXXXXXXxhcudhid

    mask_hash:返回字符串的hash编码

    1. select mask_hash('12312ASUSUUchcudhid',7);
    2. -- ca609eafd58f6d5bf4deae8680592ce7

    8.其他杂项函数

    调用Java自带的函数:java_method

    自定义函数

    1.一进一出函数 UDF普通函数 

    2.多进一出函数 UDAF聚合函数 Aggregation

    3.UDTF 表生成函数  explode一进多出

    1. select explode(`array`(1,2,2,6,3,6,334,5656,3));
    2. -- 1
    3. -- 2
    4. -- 2
    5. -- 6
    6. -- 3
    7. -- 6
    8. -- 334
    9. -- 5656
    10. -- 3

    explode函数:属于UDTF类型接受arrymap类型的数据作为输入,然后输出把每个元素变成一行

    一般可以单独使用,多半是结合业务lateral view一起使用

    输出生成一张虚表,其数据源于原表,在操作中,不能查询原表数据又想explode返回数据

    1. select explode(map('name','张三','age','18','sex','male'))
    2. -- name,张三
    3. -- age,18
    4. -- sex,male

    Hive Lateal View 侧视图

    Lateral view 是一种特殊语法 主要搭配UDTF类型一起使用,解决一些查询限制的问题

    一般使用UDTF ,就会固定搭配 Lateral VIew使用

    Lateral View 主要功能是将原本汇总在一条(行)的数据拆分成多条(行)成虚拟表,再与原表进行笛卡尔积,从而得到明细表。配合UDTF函数使用,一般情况下经常与explode函数搭配,explode的操作对象(列值)是 ARRAY 或者 MAP ,可以通过 split 函数将 String 类型的列值转成 ARRAY 来处理。

    语法: Select ..... from tableA  lateral view UDTF(****) 别名 as col1 ,col2.....

    1. create table test_01 (
    2. DEPT_NO string comment'部门编号',
    3. DEPT_TREE string comment'部门层级树',
    4. BENIFIT int comment'利润(万元)'
    5. )
    6. comment '测试-部门利润表'
    7. partitioned by (deal_date string comment '日期分区' )
    8. stored as orc;
    9. alter table test_01 drop if exists partition (DEAL_DATE='20220516');
    10. insert into table test_01 partition (DEAL_DATE='20220516')
    11. select '101','A.A1.101',50;
    12. insert into table test_01 partition (DEAL_DATE='20220516')
    13. select '102','A.A1.102',20;
    14. insert into table test_01 partition (DEAL_DATE='20220516')
    15. select '201','A.A2.201',80;
    16. select * from test_01;
    17. -- 101,A.A1.101,50,20220516
    18. -- 102,A.A1.102,20,20220516
    19. -- 201,A.A2.201,80,20220516
    20. select tmp_dept_no as DEPT_NO, sum(BENIFIT) as BENIFIT
    21. from test_01
    22. LATERAL VIEW explode (split(DEPT_TREE, '\\.')) tmp as tmp_dept_no
    23. where DEAL_DATE='20220516'
    24. group by tmp_dept_no;
    25. -- 101,50
    26. -- 102,20
    27. -- 201,80
    28. -- A,150
    29. -- A1,70
    30. -- A2,80

    聚合函数

    聚合函数属于典型多行输入一行输出也就是UDAF ,属于UDAF类型函数

    通常搭配Group by一起使用 ,对分组后进行聚合操作

    基础聚合

    内置的UDAF函数 例如 max ,min,avg,sum,通常搭配Group By一起适用

    1. drop table if exists student;
    2. create table student(
    3. num int,
    4. name string,
    5. sex string,
    6. age int,
    7. dept string)
    8. row format delimited
    9. fields terminated by ',';
    10. insert into student values (95001,'lisi','M',20,'CS');
    11. insert into student values (95002,'zhangsan','F',19,'IS');
    12. insert into student values (95003,'wangwu','M',18,'MA');
    13. insert into student values (95004,'zhaoliu','F',22,'IS');
    14. insert into student values (95005,'xiaoba','M',21,'MA');
    15. -- 统计男女人数
    16. select sex,count(*) from student group by sex;
    17. -- 统计平均年龄,人数
    18. select count(*),avg(age) from student;
    19. --搭配条件函数一起使用
    20. select
    21. sum(case when sex='M' then 1 else 0 end)
    22. from student;
    23. select
    24. sum(if(sex='M',1,0))
    25. from student;

    聚合针对null处理

    1. create table tmp1(col1 int,col2 int);
    2. insert into table tmp1 values(1,2),(null,2),(2,3);
    3. select * from tmp1;
    4. -- 空值列被忽略
    5. select sum(tmp1.col1),sum(col1+tmp1.col2) from tmp1;
    6. select
    7. sum(coalesce(col1,0)),
    8. sum(coalesce(col1,0)+col2)
    9. from tmp1;

    配合distinct去重

    在此场景下,自动设置只会启动一个MapReduce处理结果

    select count(distinct sex) from student;
    1. -- 性能优化
    2. select count(*) from
    3. (select distinct sex from student) k;
    利用struct构造数据针对应用max找出最大元素
    1. select sex,
    2. max(struct(age,name)).col1 as age,
    3. max(struct(age,name)).col2 as name
    4. from student
    5. group by sex;

    增强聚合

    常用的增强聚合函数包括:Grouping set,cube,rollup,主要适用于OLAP多为数据分析,多维指的是

    问题的角度

    1. 数据:
    2. create table if not exists cookie_info(
    3. month string,day string,cookies string
    4. )row format delimited
    5. fields terminated by ',';
    6. 2018-03,2018-03-10,cookie1
    7. 2018-03,2018-03-10,cookie5
    8. 2018-03,2018-03-12,cookie7
    9. 2018-04,2018-04-12,cookie3
    10. 2018-04,2018-04-13,cookie2
    11. 2018-04,2018-04-13,cookie4
    12. 2018-04,2018-04-16,cookie4
    13. 2018-03,2018-03-10,cookie2
    14. 2018-03,2018-03-10,cookie3
    15. 2018-04,2018-04-12,cookie5
    16. 2018-04,2018-04-13,cookie6
    17. 2018-04,2018-04-15,cookie6
    18. 2018-04,2018-04-15,cookie3
    19. 2018-04,2018-04-16,cookie1
    1. -- Grouping set
    2. select
    3. month,
    4. day,
    5. count(distinct cookies) as nums
    6. from cookie_info
    7. group by month,day
    8. grouping sets (month,day);
    9. --grouping set 把两种聚合结果做了union操作
    10. SELECT month,NULL,COUNT(DISTINCT cookies) AS nums,1 AS GROUPING__ID FROM cookie_info GROUP BY month
    11. UNION ALL
    12. SELECT NULL as month,day,COUNT(DISTINCT cookies) AS nums,2 AS GROUPING__ID FROM cookie_info GROUP BY day;
    1. SELECT
    2. month,
    3. day,
    4. COUNT(DISTINCT cookies) AS nums
    5. FROM cookie_info
    6. GROUP BY month,day
    7. GROUPING SETS (month,day,(month,day)); --1 month 2 day 3 (month,day)
    8. -- ,2018-03-10,4
    9. -- ,2018-03-12,1
    10. -- ,2018-04-12,2
    11. -- ,2018-04-13,3
    12. -- ,2018-04-15,2
    13. -- ,2018-04-16,2
    14. -- 2018-03,,5
    15. -- 2018-03,2018-03-10,4
    16. -- 2018-03,2018-03-12,1
    17. -- 2018-04,,6
    18. -- 2018-04,2018-04-12,2
    19. -- 2018-04,2018-04-13,3
    20. -- 2018-04,2018-04-15,2
    21. -- 2018-04,2018-04-16,2

    cube:表示根据Group by的维度 所有组合进行聚合 所有组合的总个数 2^n

    1. SELECT
    2. month,
    3. day,
    4. COUNT(DISTINCT cookies) AS nums
    5. FROM cookie_info
    6. GROUP BY month,day
    7. WITH CUBE;

    相当于多种维度的聚合

    1. SELECT NULL,NULL,COUNT(DISTINCT cookies) AS nums,0 AS GROUPING__ID FROM cookie_info
    2. UNION ALL
    3. SELECT month,NULL,COUNT(DISTINCT cookies) AS nums,1 AS GROUPING__ID FROM cookie_info GROUP BY month
    4. UNION ALL
    5. SELECT NULL,day,COUNT(DISTINCT cookies) AS nums,2 AS GROUPING__ID FROM cookie_info GROUP BY day
    6. UNION ALL
    7. SELECT month,day,COUNT(DISTINCT cookies) AS nums,3 AS GROUPING__ID FROM cookie_info GROUP BY month,day;

    WITH ROLLUP 以month维度进行层级聚合

    1. SELECT
    2. nvl(month,'总计'),
    3. nvl(day,'月份总计'),
    4. COUNT(DISTINCT cookies) AS nums
    5. FROM cookie_info
    6. GROUP BY month,day
    7. WITH ROLLUP;

    WITH ROLLUP 以day维度进行层级聚合

    1. SELECT
    2. nvl(month,'总计'),
    3. nvl(day,'总计'),
    4. COUNT(DISTINCT cookies) AS nums
    5. FROM cookie_info
    6. GROUP BY day,month
    7. WITH ROLLUP;

    排名分析函数

    1.基本语法

    Function (arg1,..., argn) OVER ([PARTITION BY <...>] [ORDER BY <....>]
    [])

    Function (arg1,..., argn) 可以是下面的四类函数:

    • Aggregate Functions: 聚合函数,比如:sum(...)、 max(...)、min(...)、avg(...)等

    • Sort Functions: 数据排序函数, 比如 :rank(...)、row_number(...)等

    • Analytics Functions: 统计和比较函数, 比如:lead(...)、lag(...)、 first_value(...)等

    1. CREATE TABLE IF NOT EXISTS employee (
    2. name string,
    3. dept_num int,
    4. employee_id int,
    5. salary int,
    6. type string,
    7. start_date date
    8. )
    9. ROW FORMAT DELIMITED
    10. FIELDS TERMINATED BY '|'
    11. STORED as TEXTFILE;
    12. Michael|1000|100|5000|full|2014-01-29
    13. Will|1000|101|4000|full|2013-10-02
    14. Wendy|1000|101|4000|part|2014-10-02
    15. Steven|1000|102|6400|part|2012-11-03
    16. Lucy|1000|103|5500|full|2010-01-03
    17. Lily|1001|104|5000|part|2014-11-29
    18. Jess|1001|105|6000|part|2014-12-02
    19. Mike|1001|106|6400|part|2013-11-03
    20. Wei|1002|107|7000|part|2010-04-03
    21. Yun|1002|108|5500|full|2014-01-29
    22. Richard|1002|109|8000|full|2013-09-01
    -- (1)查询姓名、部门编号、工资以及部门人数
    1. select
    2. name,
    3. dept_num as deptno ,
    4. salary,
    5. count(*) over (partition by dept_num) as cnt
    6. from employee ;

    • (2)查询姓名、部门编号、工资以及每个部门的总工资,部门总工资按照降序输出

    1. select
    2. name ,
    3. dept_num as deptno,
    4. salary,
    5. sum(salary) over (partition by dept_num order by dept_num) as sum_dept_salary
    6. from employee
    7. order by sum_dept_salary desc;

    2.窗口排序函数

    row_number:根据具体的分组和排序,为每行数据生成一个起始值等于1的唯一序列数

    rank:对组中的数据进行排名,如果名次相同,则排名也相同,但是下一个名次的排名序号会出现不连续。比如查找具体条件的topN行

    dense_rank:dense_rank函数的功能与rank函数类似,dense_rank函数在生成序号时是连续的,而rank函数生成的序号有可能不连续。当出现名次相同时,则排名序号也相同。而下一个排名的序号与上一个排名序号是连续的。

    percent_rank:排名计算公式为:(current rank - 1)/(total number of rows - 1)

    ntile:将一个有序的数据集划分为多个桶(bucket),并为每行分配一个适当的桶数。它可用于将数据划分为相等的小切片,为每一行分配该小切片的数字序号。

    • (1)查询姓名、部门编号、工资、排名编号(按工资的多少排名)

    1. select
    2. name ,
    3. dept_num as dept_no ,
    4. salary,
    5. row_number() over (order by salary desc ) rnum
    6. from employee;

    • (2)查询每个部门工资最高的两个人的信息(姓名、部门、薪水)

    1. select
    2. name,
    3. dept_num,
    4. salary
    5. from
    6. (
    7. select name ,
    8. dept_num ,
    9. salary,
    10. row_number() over (partition by dept_num order by salary desc ) rnum
    11. from employee) t1
    12. where rnum <= 2;

    • (3)查询每个部门的员工工资排名信息

    1. select
    2. name ,
    3. dept_num as dept_no ,
    4. salary,row_number() over (partition by dept_num order by salary desc ) rnum
    5. from employee;

    • (4)使用rank函数进行排名

    1. select
    2. name,
    3. dept_num,
    4. salary,
    5. rank() over (order by salary desc) rank
    6. from employee;

    • (5)使用dense_rank进行排名

    1. select
    2. name,
    3. dept_num,
    4. salary,
    5. dense_rank() over (order by salary desc) rank
    6. from employee;

    • (6)使用percent_rank()进行排名

    1. select
    2. name,
    3. dept_num,
    4. salary,
    5. percent_rank() over (order by salary desc) rank
    6. from employee;

    • (7)使用ntile进行数据分片排名

    1. SELECT
    2. name,
    3. dept_num as deptno,
    4. salary,
    5. ntile(4) OVER(ORDER BY salary desc) as ntile
    6. FROM employee;

    从 Hive v2.1.0开始, 支持在OVER语句里使用聚集函数,比如

    1. SELECT
    2. dept_num,
    3. row_number() OVER (PARTITION BY dept_num ORDER BY sum(salary)) as rk
    4. FROM employee
    5. GROUP BY dept_num;

    窗口分析函数

    常用的分析函数主要包括:

    • cume_dist

    如果按升序排列,则统计:小于等于当前值的行数/总行数(number of rows ≤ current row)/(total number of rows)。如果是降序排列,则统计:大于等于当前值的行数/总行数。比如,统计小于等于当前工资的人数占总人数的比例 ,用于累计统计。

    • lead(value_expr[,offset[,default]])

    用于统计窗口内往下第n行值。第一个参数为列名,第二个参数为往下第n行(可选,默认为1),第三个参数为默认值(当往下第n行为NULL时候,取默认值,如不指定,则为NULL

    • lag(value_expr[,offset[,default]]):

    与lead相反,用于统计窗口内往上第n行值。第一个参数为列名,第二个参数为往上第n行(可选,默认为1),第三个参数为默认值(当往上第n行为NULL时候,取默认值,如不指定,则为NULL)

    • first_value: 取分组内排序后,截止到当前行,第一个值

    • last_value

    取分组内排序后,截止到当前行,最后一个值

    • (1)统计小于等于当前工资的人数占总人数的比例

      1. SELECT
      2. name,
      3. dept_num as deptno,
      4. salary,
      5. cume_dist() OVER (ORDER BY salary) as cume
      6. FROM employee;

    • (2)统计大于等于当前工资的人数占总人数的比例

    1. SELECT
    2. name,
    3. dept_num as deptno,
    4. salary,
    5. cume_dist() OVER (ORDER BY salary desc) as cume
    6. FROM employee;

    按照部门统计小于等于当前工资的人数占部门总人数的比例

    1. SELECT
    2. name,
    3. dept_num as deptno,
    4. salary,
    5. cume_dist() OVER (PARTITION BY dept_num ORDER BY salary) as cume
    6. FROM employee;

    • (4)按部门分组,统计每个部门员工的工资以及大于等于该员工工资的下一个员工的工资

    1. SELECT
    2. name,
    3. dept_num as deptno,
    4. salary,
    5. lead(salary,1) OVER (PARTITION BY dept_num ORDER BY salary) as lead
    6. FROM employee;

    • (5)按部门分组,统计每个部门员工的工资以及小于等于该员工工资的上一个员工的工资

    1. SELECT
    2. name,
    3. dept_num as deptno,
    4. salary,
    5. lag(salary,1) OVER (PARTITION BY dept_num ORDER BY salary) as lead
    6. FROM employee;

    (6)按部门分组,统计每个部门员工工资以及该部门最低的员工工资

    1. SELECT
    2. name,
    3. dept_num as deptno,
    4. salary,
    5. first_value(salary) OVER (PARTITION BY dept_num ORDER BY salary) as fval
    6. FROM employee;

    • (7)按部门分组,统计每个部门员工工资以及该部门最高的员工工资

    1. SELECT
    2. name,
    3. dept_num as deptno,
    4. salary,
    5. last_value(salary) OVER (PARTITION BY dept_num ORDER BY salary RANGE
    6. BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as lval
    7. FROM employee;

    注意:last_value默认的窗口是RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW,表示当前行永远是最后一个值,需改成RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING。

    • RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

    为默认值,即当指定了ORDER BY从句,而省略了window从句 ,表示从开始到当前行。

    • RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING

    表示从当前行到最后一行

    • RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING

    表示所有行

    • n PRECEDING  m FOLLOWING

    表示窗口的范围是:[(当前行的行数)- n, (当前行的行数)+m]

  • 相关阅读:
    信息系统建设和服务能力评估证书CS
    Android-使用StaticLayout实现icon跟随TextView末尾效果
    使用 Python为你的在线会议创建一个假的摄像头
    【SwiftUI模块】0051、SwiftUI搭建Note笔记应用程序UI,适用于iOS和macOS平台
    python之value_counts()介绍
    什么是单点登录?什么又是 OAuth2.0?
    MS SQL Server partition by 函数实战 统计与输出
    02 Shell编程之条件语句
    代理模式详解
    向量数据库Annoy和Milvus
  • 原文地址:https://blog.csdn.net/ziaoming_biu/article/details/137919192