直接类似这样写吧,最后只要1的结果就可以了,我使用的时oracel,你改成类似你的数据库语法就行了
with tmp as (
select 'a店铺' as dpname,'2022-01-15' as mytime, 15 as value_s from dual
union all
select 'a店铺' as dpname,'2022-02-15' as mytime, 30 as value_s from dual
union all
select 'a店铺' as dpname,'2022-03-06' as mytime, 16 as value_s from dual
union all
select 'a店铺' as dpname,'2022-06-18' as mytime, 25 as value_s from dual
union all
select 'b店铺' as dpname,'2022-04-12' as mytime, 100 as value_s from dual
)
select
a.*,
b.minmytime as "发货最小时间",
case when to_date(a.mytime,'yyyy-mm-dd')<=add_months(to_date(b.minmytime,'yyyy-mm-dd'),3) then 1 else 0 end as "是否需要"
from tmp a
left join
(
select dpname,min(mytime) as minmytime from tmp
group by dpname) b on a.dpname=b.dpname
a店铺最小发货时间2022-01-15,那么往后推3个月是2022-04-15,那么最后2022-06-18这一个数据就不要了。你再套一个子查询就可以了。。
[思路是把每个店铺的的最小发货时间取出来。再用店铺名称关联一下,然后把最小发货时间+3个月后与它每个的时间对比一个,这样多一个辅助列,最后就只取是否需要为1的就可以了]
子查询前
子查询后