开发者

Update Select based on Aggregate Function "SUM":

开发者 https://www.devze.com 2023-04-03 13:04 出处:网络
I have 3 tables which are given below: PODetail: PODetailIDPOIDItemIDPOQuantityStatusID ------------------------------------------------

I have 3 tables which are given below:

PODetail:

      PODetailID       POID     ItemID      POQuantity        StatusID
     ------------     ------   --------    ------------      ----------
          1             1         10          8.00              2
          2             1         17          33.00             2
          3             2         1           7.00              2

MRVDetail:

      MRVDetailID       AcceptedQty         PODetailID
     --------------    -------------       ------------
          1               3.00                  3
          2               4.00                  3
          3               10.00                 4

@Detail: //It is a table variable

      PODetailID           POQuantity
     ------------         ------------
          3                   7

Here, Based on the above 3 tables. I want to update the PODetail StatusID = 4 when SUM(AcceptedQty) of the MRVDetail for the PODetailID = POQuantity of the table variable @POD开发者_StackOverflow中文版etail to the particular PODetailID.

How to achieve this?

I have tried the below Update Statement. But it is not at all working. How to do this? Please help me.

    UPDATE 
    PODetail
SET 
    PODetail.StatusID = 4
FROM 
        PODetail 
INNER JOIN @Detail P ON PODetail.PODetailID = P.PODetailID
INNER JOIN MRVDetail ON MRVDetail.PODetailID = P.PODetailID
WHERE 
(SELECT SUM(AcceptedQty) FROM MRVDetail WHERE MRVDetail.PODetailID = P.PODetailID) = P.POQuantity 
    AND PODetail.POID = 2


UPDATE PODetail
SET    StatusID = 4
WHERE  PODetailID IN (SELECT d.PODetailID
                      FROM   @Detail AS d
                      INNER JOIN (SELECT PODetailID,
                                         SUM(AcceptedQty) AS Qty
                                  FROM   MRVDetail
                                  GROUP BY PODetailID) AS m
                      ON d.PODetailID = m.PODetailID AND 
                         d.POQuantity = m.Qty)  

https://data.stackexchange.com/stackoverflow/q/112195/

0

精彩评论

暂无评论...
验证码 换一张
取 消