简体   繁体   中英

Update Statement with Sub-query

I have the following query:

DECLARE @fromdate date = '2016-09-01'
DECLARE @todate date = '2016-10-03'

SELECT 
    inventoryKey,
    SUM(DATEDIFF(DAY, transdate, @todate) * qty) AS daysavailable
FROM
    (SELECT
         InventoryKey,
         CONVERT(date, invch.transactiondate) AS transdate, 
         SUM(invch.additionqty + invch.SubtractionQty) AS qty
     FROM
         dw.dbo.inventorychange invch
     WHERE
         (TransactionDate IS NOT NULL OR TransactionType <> '')
         AND TransactionType NOT IN ('TRANSFER IN', 'TRANSFER OUT', 'BALANCE', 'LEASED')
         AND InventoryKey = 'A000HV9T'
         AND TransactionDate BETWEEN @fromdate AND @todate
     GROUP BY 
         invch.TransactionDate, InventoryKey) AS a
GROUP BY 
    InventoryKey

This works completely fine on its own. However, I am creating a stored procedure which will perform a number of calculations, one of them being the calculation above. The stored procedure will return a table, where this column will be updated with the actual value. However, I don't entirely know how to accurately translate that in a sub-query update statement.

Here is what I have below:

declare @inventorychanges table
(
    inventorykey    nvarchar(10),
    warehouse       nvarchar(10),
    transdate       date,
    trantype        nvarchar(25),
    additionqty     numeric,
    subtractionqty  numeric

)
insert into @inventorychanges (inventorykey, warehouse, transdate, trantype, additionqty, subtractionqty)
select invch.inventorykey, invch.warehouse, invch.TransactionDate, invch.TransactionType, invch.additionqty, invch.SubtractionQty
from dw.dbo.inventorychange invch
join @icodes ic on ic.inventorykey = invch.inventorykey

WHERE invch.TransactionDate BETWEEN @fromdate AND @todate
and invch.warehouse = ISNULL(@location, '')
AND (TransactionDate IS NOT NULL OR TransactionType <> '')
AND TransactionType NOT IN ('TRANSFER IN' ,'TRANSFER OUT', 'BALANCE', 'LEASED')


/* Update Days Available*/
UPDATE ic
SET ic.daysavaialblepsgear = (
                                SELECT SUM(invch.additionqty + invch.subtractionqty) 
                                    )
FROM @icodes ic, @inventorychanges invch 
where invch.inventorykey = ic.inventorykey

This however, doesn't seem to be working as it should be. Can anyone point me in the right direction as to how I can accomplish this.

Here is one way using CTE

DECLARE @fromdate date = '2016-09-01'
DECLARE @todate date = '2016-10-03'
;with cte as
(
SELECT 
    inventoryKey,
    SUM(DATEDIFF(DAY, transdate, @todate) * qty) AS daysavailable
FROM
    (SELECT
         InventoryKey,
         CONVERT(date, invch.transactiondate) AS transdate, 
         SUM(invch.additionqty + invch.SubtractionQty) AS qty
     FROM
         dw.dbo.inventorychange invch
     WHERE
         (TransactionDate IS NOT NULL OR TransactionType <> '')
         AND TransactionType NOT IN ('TRANSFER IN', 'TRANSFER OUT', 'BALANCE', 'LEASED')
         AND InventoryKey = 'A000HV9T'
         AND TransactionDate BETWEEN @fromdate AND @todate
     GROUP BY 
         invch.TransactionDate, InventoryKey) AS a
GROUP BY 
    InventoryKey
)
UPDATE ic
SET ic.daysavaialblepsgear = invch .daysavailable
FROM @icodes ic 
Join CTE invch 
  ON invch.inventorykey = ic.inventorykey

You can also place the entire sub-query in place of CTE in JOIN

UPDATE ic 
SET    ic.daysavaialblepsgear = invch .daysavailable 
FROM   @icodes ic 
       JOIN (SELECT inventorykey, 
                    Sum(Datediff(day, transdate, @todate) * qty) AS 
                    daysavailable 
             FROM   (SELECT inventorykey, 
                            CONVERT(DATE, invch.transactiondate)          AS 
                            transdate, 
                            Sum(invch.additionqty + invch.subtractionqty) AS qty 
                     FROM   dw.dbo.inventorychange invch 
                     WHERE  ( transactiondate IS NOT NULL 
                               OR transactiontype <> '' ) 
                            AND transactiontype NOT IN ( 
                                'TRANSFER IN', 'TRANSFER OUT', 
                                'BALANCE', 
                                'LEASED' 
                                                       ) 
                            AND inventorykey = 'A000HV9T' 
                            AND transactiondate BETWEEN @fromdate AND @todate 
                     GROUP  BY invch.transactiondate, 
                               inventorykey) AS a 
             GROUP  BY inventorykey) invch 
         ON invch.inventorykey = ic.inventorykey 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM