你为了得到拆放同
select * from activities where activity = 1
,你可能已经猜到如何获得订单排放:-)
所以将二者结合起来,只保留行与过高的区别:
select p.order_nr, p.date as placed, d.date as discharged
from (select * from activities where activity = 1) p
join (select * from activities where activity = 2) d
on d.order_nr = p.order_nr and datediff(d.date, p.date) > 1;
你可以得到相同的,每个订单的集合:
select
order_nr,
any_value(case when activity = 2 then date end) as placed,
any_value(case when activity = 1 then date end) as discharged
from activities
group by order_nr
having datediff(any_value(case when activity = 2 then date end),
any_value(case when activity = 1 then date end)) > 1;
如果您要包括未结订单,你会做几乎相同的。对于没有解除记录的订单,可能会在今天输入,因此昨天发出的订单可能仍然正常,而之前发出的订单已经过久。所以如果没有dicharged记录,我们想假装有一个date = today。
查询#1:
select p.order_nr, p.date as placed, d.date as discharged
from (select * from activities where activity = 1) p
left join (select * from activities where activity = 2) d
on d.order_nr = p.order_nr and datediff(coalesce(d.date, curdate()), p.date) > 1;
查询#2:
select
order_nr,
any_value(case when activity = 2 then date end) as placed,
any_value(case when activity = 1 then date end) as discharged
from activities
group by order_nr
having datediff(any_value(case when activity = 2 then date end),
coalesce(any_value(case when activity = 1 then date end), curdate())) > 1;
感谢Siyual用于在格式化我的问题!问候,赛 – Sai