SQL每日一题(20210720)
SQL每日一题(20211014)
SQL每日一题(20220408)
select t.UID,
t.callback_date,
lag(t.callback_date) over (partition by t.UID order by t.callback_date) c,
case
when t.callback_date - lag(t.callback_date) over (partition by t.UID order by t.callback_date) >= 7 then '计费'
else '不计费' end as charge
from t0407 t;
with ct as (select a.xh, a.UID, a.CALLBACK_DATE 时间2, b.CALLBACK_DATE 时间1
from (select *, ROW_NUMBER() over (order by curdate()) xh from T0407) a
left join (select *, ROW_NUMBER() over (order by curdate()) xh from T0407) b
on a.xh = b.xh + 1 and a.UID = b.UID)
select lsb.UID, lsb.时间2 CALLBACK_DATE, (case when 时间差 >= 7 then '计费' else '不计费' end) charge
from (select *, timestampdiff(day, ct.时间1, ct.时间2) 时间差 from ct) lsb