我有一个用户表,一个级别表,一个提交订单和处理订单的表。
提交的订单如下所示:
OrderId UserId Level_Name Discounted_Price Order_Date Price
1 1 OLE Core 0 2020-11-01 00:00:00.000 19.99
2 1 Xandadu 1 2020-11-01 00:00:00.000 0
3 2 Xandadu 0 2020-12-05 00:00:00.000 5
4 1 Eldorado 1 2021-01-31 00:00:00.000 9
5 2 Eldorado 0 2021-02-20 00:00:00.000 10
6 2 Birmingham Blues NULL 2021-07-10 00:00:00.000 NULL我想做的是:
UserId 2有伯明翰蓝军的订单,他们已经订购了Eldorado,因此有资格享受伯明翰蓝军订单的折扣。是否有方法检查整个表的这种相似性,如果它存在,更新折扣价格为1,并改变价格,让我们为伯明翰蓝调订单10。
编辑:我已经对游标的使用进行了研究,我确信游标会完成这项工作,但它们看起来很复杂,并且希望有一个更简单的解决方案。许多线程似乎也避免使用游标。我还研究了这个问题:T-SQL: Deleting all duplicate rows but keeping one,并认为我可以在某种程度上使用这个问题的答案。
发布于 2022-01-25 21:31:12
根据您的描述和进一步的注释,希望以下内容能够满足您的要求--为指定的用户更新当前值为NULL且用户具有符合条件的现有顺序的行:
update s set
s.Discounted_Price = 1,
Price = 10
from submitted_Orders s
where s.userId=2
and s.Level_Name = 'Birmingham Blues'
and s.discounted_Price is null
and s.Price is null
and exists (
select * from submitted_orders so
where so.userId = s.userId
and so.Level_name = 'Eldorado'
and so.Order_Date < s.OrderDate
);https://stackoverflow.com/questions/70855549
复制相似问题