Hive - - 分组求最大,最小(加行键)

本文介绍如何使用Hive SQL进行数据分组并求取每组的最大值和最小值,通过添加行号进行排序筛选,同时展示了如何为查询结果添加排序标识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                            Hive - - 分组求最大,最小(加行键)

数据:


1325927 陕西 汉中 084 0849
1325928 陕西 汉中 084 0849
1325930 陕西 延安 084 0842
1325931 陕西 延安 084 0842
1325933 陕西 榆林 084 0845
1325935 陕西 榆林 084 0845
1325941 陕西 西安 084 0841
1325943 陕西 西安 084 0841
1325944 陕西 西安 084 0841
1325946 陕西 西安 084 0841
1325947 陕西 西安 084 0841
1325948 陕西 西安 084 0841
1325949 陕西 西安 084 0841

建表,添加数据

create table if not exists tab4(

  no        string,
  province  string,
  city      string,
  pid     string,
  cid     string
)
row format delimited fields terminated by '\t'
stored as textfile;


load data local inpath '/home/data/data4.txt' into table tab4;




求最大
select t.*
from(
select no,province,city,pid,cid,row_number() over(partition by province,city order by pid desc) rk
from tab4) t
where t.rk=1;


求最小:

select t.*
 from(
select no,province,city,pid,cid,row_number() over(partition by province,city order by pid) rk
from tab4) t
where t.rk=1;


查询所有省份,并且分组给city标识列进行排序

select *,row_number() over(partition by province,city order by city) rk
from tab4;



posted on 2018-07-09 22:51 meiLinYa 阅读( ...) 评论( ...) 编辑 收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值