Sql 如何否定无效的订单数据?
我有一个有趣的订单历史记录表,如下所示:如果用户取消订单,系统不会更新原始订单,但新订单将在同一日期输入负数量和负成本,如:Sql 如何否定无效的订单数据?,sql,sql-server,Sql,Sql Server,我有一个有趣的订单历史记录表,如下所示:如果用户取消订单,系统不会更新原始订单,但新订单将在同一日期输入负数量和负成本,如: user_id date qty cost 1 2018-10-01 2 100.00 -- a wrong order 1 2018-10-01 -2 -100.00 -- negate the above order 1 2018-10-01 2 100.00 -- thi
user_id date qty cost
1 2018-10-01 2 100.00 -- a wrong order
1 2018-10-01 -2 -100.00 -- negate the above order
1 2018-10-01 2 100.00 -- this is a valid order
1 2018-10-01 3 150.00 -- this is a valid order
1 2018-10-01 1 50.00 -- this is a valid order
如何创建仅包含以下有效顺序的清理表:
user_id date qty cost
1 2018-10-01 2 100.00
1 2018-10-01 3 150.00
1 2018-10-01 1 50.00
我尝试使用“按日期分组”,但这并不好,因为它可能会在一天内挤压多个有效订单
如果有人能直接在sql server中帮我做这件事,那将非常方便!否则,我只需编写一个python脚本来加载数据并在外部执行此操作…这不是一个好的设计。您确实需要一个
订单id
。原始解决方案是不存在
:
select o.*
from orders o
where not exists (select 1 from orders o2 where o2.user_id = o.user_id and o2.date = o.date and o2.qty = - o.qty and o2.cost = - o.cost);
唉,有人可能一天订购两次,然后只取消其中一次。所以,你们需要一个计数器。因此,使用行编号()
:
这不是一个好的设计。您确实需要一个
订单id
。原始解决方案是不存在
:
select o.*
from orders o
where not exists (select 1 from orders o2 where o2.user_id = o.user_id and o2.date = o.date and o2.qty = - o.qty and o2.cost = - o.cost);
唉,有人可能一天订购两次,然后只取消其中一次。所以,你们需要一个计数器。因此,使用行编号()
:
您的订单表没有
OrderNumber
字段?不存在OrderNumber。如果我有类似的情况,就不会有任何问题……系统如何区分上面列出的第一行和第三行?每行都有不同的行id。这些行都没有不同的id。您有一个没有OrderNumber
字段的订单表?不存在OrderNumber。如果我有类似的问题,就不会有任何问题……系统如何区分上面列出的第一行和第三行?每行都有不同的行id。这些行都没有不同的id