我有一组相对简单的数据,如下所示:
invoice_id created_at amount_in_cents user_id
22348 2019-11-07 550 31773927
22349 2019-11-08 -550 31773927
22498 2019-11-10 -3400 2389483
22499 2019-11-10 3400 2389483
22500 2019-11-11 18000 93842938正如您所看到的,样本数据的前两行被归因于相同的user_id,但它们的值是相反的(加起来为0)。第3行和第4行也是如此。我想在30天内删除所有针对同一用户的反向发票,只留下第五行。
我可以用python来完成这个任务,但是它会大大扩展这个过程。有什么简单的方法可以用SQL来完成这个任务吗?
发布于 2020-01-21 22:27:54
您可以在关联子查询中使用not exists:
select t.*
from mytable t
where not exists (
select 1
from mytable t1
where
t1.user_id = t.user_id
and greatest(t1.created_at, t.created_at)
<= least(t1.created_at, t.created_at) + interval '30 days'
and t1.amount_in_cents = - t.amount_in_cents
)not exists条件确保同一用户在30天内不存在其他记录,并且数量与之相反。
发布于 2020-01-21 22:42:15
我认为这个问题没有简单的解决办法。如果要删除所有匹配对,则可以枚举和删除:
select min(invoice_id), min(created_at), user_id, max(amount_in_cents) as amount_in_cents
from (select t.*,
row_number() over (partition by user_id, amount_in_cents order by created_at) as seqnum
from t
) t
group by abs(amount_in_cents), user_id, seqnum
having count(*) = 1; -- only one "matching" amount但是,30天的限制是很有挑战性的,我认为您可能需要一个递归的CTE。
考虑以下数据:
1 jan 1 500
1 jan 15 500
1 feb 1 -500
1 feb 10 -500你想要什么结果?
https://stackoverflow.com/questions/59849907
复制相似问题