使用子查询更新语句

时间:2016-10-04 15:45:10

标签: sql sql-server tsql

我有以下查询:

UPDATE my_table SET results = ???.

这本身就完全没问题。但是,我正在创建一个存储过程,它将执行许多计算,其中一个是上面的计算。存储过程将返回一个表,此列将使用实际值进行更新。但是,我并不完全知道如何在子查询更新语句中准确地翻译它。

以下是我的内容:

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
然而,这似乎并没有按照应有的方式发挥作用。 任何人都可以指出我是如何做到这一点的正确方向。

1 个答案:

答案 0 :(得分:2)

以下是使用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

您还可以在CTE

中放置整个子查询来代替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