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

 

相关文章:

  • 2021-09-26
  • 2022-01-04
  • 2021-07-03
  • 2021-10-20
  • 2022-02-18
  • 2021-10-09
  • 2021-12-30
  • 2022-01-19
猜你喜欢
  • 2022-01-09
  • 2021-11-16
  • 2021-08-08
  • 2022-12-23
  • 2021-08-20
  • 2021-12-18
  • 2022-01-03
相关资源
相似解决方案