with base_data as( SELECT '1' as gid, 4 as price, '2023-08-01' as dt UNION all SELECT '1' as gid , 3 as price, '2023-08-02' as dt UNION all SELECT '1' as gid , 2 as price, '2023-08-03' as dt UNION all SELECT '1' as gid , 3 as price, '2023-08-04' as dt ) select gid,price,dt,days as day -- 最近day天最小值 from ( select gid,price,dt,b_price,dt2,rn, COUNT() over(partition by gid,price,dt,res) as days from ( select gid,price,dt,b_price,dt2,rn,date_add('day',-rn,CAST(dt2 AS date)) as res from ( SELECT a.gid,a.price,a.dt,b.price as b_price,b.dt as dt2, row_number() over(partition by a.gid,a.dt order by b.dt) as rn from base_data as a left join base_data as b on a.dt >= b.dt where a.price <= b.price ) as t1 ) as t2 ) as t3 where dt = dt2 -- 不等值关联,过滤大于这个价格的数据,最后计算连续天数,最后再取最近的连续数据。
点赞 评论

相关推荐

不愿透露姓名的神秘牛友
11-24 20:55
阿里国际 Java工程师 2.7k*16.0
程序员猪皮:没有超过3k的,不太好选。春招再看看
点赞 评论 收藏
分享
10-15 15:00
潍坊学院 golang
跨考小白:这又不是官方
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
牛客网
牛客企业服务