使用 Group By 更新查询

Update Query With Group By

我知道您不能在更新查询中使用聚合 - 但我无法理解如何编写此更新的语法。这是示例数据

UserID   Item            InStock     R2S
14         S0            Yes
28         M23           No
10         Ca2           No
14         G01           No
16         G21           Yes
16         G33           Yes

如果所有商品的 InStock 都是 yes 而不是将 R2S 更新为 yes

我尝试了下面的语法,但是它没有考虑具有 14 和 16 等倍数的用户 ID,它只是逐行查看。

由于两个项目都是 r2s,我怎样才能将其更改为仅将用户 ID 16 更新为是?

UPDATE send SET R2S = 'Yes' WHERE (((InStock)='Yes'));

这是 SQL 服务器,但它应该可以工作或接近。

update send
set R2S = 'Yes'
from send
join (select UserId
      from send
      group by UserId
      having min(InStock) = 'Yes') Maxes
on send.UserId = Maxes.UserId

子查询消除任何具有 'No' 的人,然后我们加入那个。

我不喜欢数据点位于 2 个位置(事实上没有 "No"s)。查询应该在您准备好使用它时完成。

下一个 reader 可能更容易遵循的另一种方法:

    update send
    set R2S = 'Yes'
    from send
    where UserId not in (select UserId
          from send
          where InStock = 'No')