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

Hive练习题之窗口函数构造辅助列_第1张图片建表

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

导入数据

vim /doit/tb_ab
2010,1
2011,1
2012,1
2013,0
2014,0
2015,1
2016,1
2017,1
2018,0
2019,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 d
from (
select  a,b,a - row_number() over(partition by b order by a ) as c
from tb_ab
)t

使用mysql变量

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

你可能感兴趣的:(Hive,hive,hadoop,数据仓库)