1. 程式人生 > 實用技巧 >Spark專案實戰從0到1之(15)hive實現連續三個月學生成績都為A的記錄

Spark專案實戰從0到1之(15)hive實現連續三個月學生成績都為A的記錄

1.資料
s1,201801,A
s1,201802,A
s1,201803,C
s1,201804,A
s1,201805,A
s1,201806,A
s2,201801,A
s2,201802,B
s2,201803,C
s2,201804,A
s2,201805,D
s2,201806,A
s3,201801,C
s3,201802,A
s3,201803,A
s3,201804,A
s3,201805,B
s3,201806,A
2.建表
create table if not exists student(name string,month string,degree string)
row format delimited
fields terminated 
by ',' ; load data local inpath '/root/stu.txt' into table student; 3.現要查詢表中連續三個月以上degree均為A的記錄? select a1.name, a1.month, a1.degree from ( select name, month, degree, sum(if(degree = 'A', 1, 0)) OVER(PARTITION BY name ORDER BY month ROWS BETWEEN 2 PRECEDING AND
CURRENT ROW) AS score1, sum(if(degree = 'A', 1, 0)) OVER(PARTITION BY name ORDER BY month ROWS BETWEEN 1 PRECEDING AND 1 following) AS score2, sum(if(degree = 'A', 1, 0)) OVER(PARTITION BY name ORDER BY month ROWS BETWEEN CURRENT ROW AND 2 following) AS score3 from student ) as a1
where a1.score1 = 3 or a1.score2 = 3 or a1.score3 = 3 結果: s1 201804 A s1 201805 A s1 201806 A s3 201802 A s3 201803 A s3 201804 A