这里已经检索出初始数据,按照name分类,同时日期升序排列。 接下来需要判断连续日期,这时候可以假设日期都是连续且递增,row number也是连续和递增。所以2者之差,应该一致。如果日期有断开,则2者之差就不会一样。
进行日期的相减,需要查询函数,进行日期处理的,资料如下 show functions; desc function date_sub ; +----------------------------------------------------+ | tab_name | +----------------------------------------------------+ | date_sub(start_date, num_days) - Returns the date that is num_days before start_date. | +----------------------------------------------------+ 进一步对数据进行处理 select *, date_sub(ctime, rn) as date_sub from (select *, row_number() over(partition by name order by ctime) rn from tb_shop) t1; +----------+-------------+----------+--------+-------------+ | t1.name | t1.ctime | t1.cost | t1.rn | date_sub | +----------+-------------+----------+--------+-------------+ | a | 2020-02-05 | 200.0 | 1 | 2020-02-04 | | a | 2020-02-06 | 300.0 | 2 | 2020-02-04 | | a | 2020-02-07 | 200.0 | 3 | 2020-02-04 | | a | 2020-02-08 | 400.0 | 4 | 2020-02-04 | | a | 2020-02-10 | 600.0 | 5 | 2020-02-05 | | a | 2020-03-01 | 200.0 | 6 | 2020-02-24 | | a | 2020-03-02 | 300.0 | 7 | 2020-02-24 | | a | 2020-03-03 | 200.0 | 8 | 2020-02-24 | | a | 2020-03-04 | 400.0 | 9 | 2020-02-24 | | a | 2020-03-05 | 600.0 | 10 | 2020-02-24 | | b | 2020-02-05 | 200.0 | 1 | 2020-02-04 | | b | 2020-02-06 | 300.0 | 2 | 2020-02-04 | | b | 2020-02-08 | 200.0 | 3 | 2020-02-05 | | b | 2020-02-09 | 400.0 | 4 | 2020-02-05 | | b | 2020-02-10 | 600.0 | 5 | 2020-02-05 | | c | 2020-01-31 | 200.0 | 1 | 2020-01-30 | | c | 2020-02-01 | 300.0 | 2 | 2020-01-30 | | c | 2020-02-02 | 200.0 | 3 | 2020-01-30 | | c | 2020-02-03 | 400.0 | 4 | 2020-01-30 | | c | 2020-02-10 | 600.0 | 5 | 2020-02-05 | +----------+-------------+----------+--------+-------------+ 根据date_sub字段进行计数,超过3次的就是需要的数据 select name, date_sub, count(1) as count from (select *, date_sub(ctime, rn) as date_sub from (select *, row_number() over(partition by name order by ctime) rn from tb_shop) t1) t2 group by name, date_sub having count > 3; # 注意这里别名t1 t2不要遗漏,否则会导致变量访问出问题 +-------+-------------+--------+ | name | date_sub | count | +-------+-------------+--------+ | a | 2020-02-04 | 4 | | a | 2020-02-24 | 5 | | c | 2020-01-30 | 4 | +-------+-------------+--------+ 需要进一步解析,再进一步根据名字区分,distinct name select distinct name from (select name, date_sub, count(1) as count from (select *, date_sub(ctime, rn) as date_sub from (select *, row_number() over(partition by name order by ctime) rn from tb_shop) t1) t2 group by name, date_sub having count > 3) t3; +-------+ | name | +-------+ | a | | c | +-------+