欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Hive练习题之窗口函数构造辅助列

时间:2023-07-11

建表

create table if not exists test.tb_ab(A string,B int)row format delimited fields terminated by ",";

导入数据

vim /doit/tb_ab2010,12011,12012,12013,02014,02015,12016,12017,12018,02019,0

load data local inpath "/doit/tb_ab" into table test.tb_ab;

解法:

使用窗口函数

select a,b,c,row_number() over(partition by b,c order by a) as dfrom (select a,b,a - row_number() over(partition by b order by a ) as cfrom tb_ab)t

使用mysql变量

SELECt A ,B ,CFROM( SELECt A ,B ,@C := CASE WHEN @B = B THEN @C + 1 ELSE 1 END AS C ,@B := B AS B1 FROM tb_ab ) t

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。