【问题标题】:How to update the column using two tables based on conditions?如何根据条件使用两个表更新列?
【发布时间】:2011-07-29 04:43:20
【问题描述】:

我有两个表,例如 STR_IndentHeader 和 STR_IndentDetail。

STR_IndentDetail:

  IndentID         ItemID           ItemStatusID
  --------         ------           ------------
    1                22                 4
    1                23                 4
    2                11                 4
    2                12                 3
    2                13                 3

STR_IndentHeader:

  IndentID           StatusID
  --------          -----------
    1                  1
    2                  1

这里我要更新STR_IndentHeader StatusID = 4,当所有STR_IndentDetail.ItemID 的ItemStatusID = 4 相对于IndentID。否则我想更新 STR_IndentHeader.StatusID = 3。

在上表中,在 STR_IndentDetail 中,对于 IndentID“1”,所有 Items 的 ItemStatusID 都是 4。所以我们有 Update STR_IndentHeader.StatusID = 4。 但是对于 IndentID “2”,一个 Item 的(即 ItemID=11)ItemStatusID = 4 和 Remaining 两个 ItemStatusID = 3。所以在这种情况下,我们必须更新 STR_IndentHeader.StatusID = 3。 我希望它能提供更好的想法。如何做到这一点?

我对上述表格的期望结果是这样的:

STR_IndentHeader:

  IndentID           StatusID
  --------          -----------
    1                  4
    2                  3   

【问题讨论】:

  • 我看不出 PUR_POIndent 与此有什么关系。您只想为 STR_IndentDetail 中的每个 IndentID 提供 MIN(ItemStatusID),还是巧合?
  • 这里,我将 POID 作为参数传递。我需要使用 STR_IndentDetail 根据 PUR_POIndent 表检查项目和缩进,然后更新 STR_IndentHeader。
  • 您在 STR_IndentDetail 中将 STR_IndentHeader 的 StatusID 设置为与之关联的 MIN(ItemStatusID) 是巧合,还是您想用它来更新它?
  • 不,那不是 MIN(ItemStatusID),我想用 StatusID = 3 更新 STR_IndentHeader

标签: sql sql-server sql-server-2005 sql-update


【解决方案1】:

根据提供的信息,我假设您希望 STR_IndentHeader 中的 statusID 是该 IndentID 的 STR_IndentDetail 中的最小 ItemStatusID 值。

如果是这种情况,请尝试以下方法:

update STR_IndentHeader
set statusid = minitemstatusid
from
    (select indentid,MIN(itemstatusid) as minitemstatusid
    from STR_IndentDetail  
    group by indentid) id 
where id.IndentID = STR_IndentHeader.indentid

编辑:

如果你想静态应用 3 的 ItemStatusID,如果 statusID 不是 4,则基于 cmets:

update STR_IndentHeader
set statusid = case minitemstatusid when 4 then 4 else 3 end 
from
    (select indentid,MIN(itemstatusid) as minitemstatusid
    from STR_IndentDetail  
    group by indentid) id 
where id.IndentID = STR_IndentHeader.indentid

【讨论】:

    【解决方案2】:

    这是使用 SQL Server 2005 中提供的CROSS APPLY 执行此操作的一种方法。希望对您有所帮助。

    UPDATE      SH
    SET         SH.StatusID = (CASE WHEN DC.DistinctCount = 1 THEN 4 ELSE 3 END)
    FROM        dbo.STR_IndentHeader    SH
    CROSS APPLY (
                    SELECT      SD.IndentID
                            ,   COUNT(DISTINCT ItemStatusID)  AS DistinctCount
                    FROM        dbo.STR_IndentDetail    SD
                    WHERE       SH.IndentID             = SD.IndentID
                    GROUP BY    SD.IndentID
                ) DC
    

    【讨论】:

    • 此查询还会更新 Header 表中 IndentID 2 的 StatusID
    • 忽略 PUR_POIndent 表。根据 STR_IndentDetail 和 STR_IndentHeader 更新 StatusID。请稍候,我正在编辑我的问题,我希望它能提供更好的想法。
    • 不,我不是在寻找通用查询。我只是特定于 3 和 4。
    • 谢谢湿婆。到目前为止,我还没有使用过 CROSS APPLY,但现在我对它有了一些想法。
    【解决方案3】:

    当我使用您的示例数据设置数据库时,这对我有用:

    UPDATE STR_IndentHeader ih
    SET StatusID = (SELECT MIN(ItemStatusID) FROM STR_IndentDetail id WHERE id.IndentID = ih.IndentID)
    WHERE IndentID IN (SELECT DISTINCT IndentID FROM PUR_POIndent WHERE POID = 8)
    

    【讨论】:

    • 它会抛出错误,例如ih附近的语法错误以及关键字“where”附近的语法错误
    猜你喜欢
    • 1970-01-01
    • 2011-07-01
    • 2022-12-03
    • 2020-06-25
    • 1970-01-01
    • 2013-01-21
    • 2018-12-11
    • 1970-01-01
    • 2022-11-25
    相关资源
    最近更新 更多