久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

具有累積值的 SQL 更新表

SQL Update table with cumulative value(具有累積值的 SQL 更新表)
本文介紹了具有累積值的 SQL 更新表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有這張桌子:

Date  |StockCode|DaysMovement|OnHand
29-Jul|SC123    |30          |500
28-Jul|SC123    |15          |NULL
27-Jul|SC123    |0           |NULL
26-Jul|SC123    |4           |NULL
25-Jul|SC123    |-2          |NULL
24-Jul|SC123    |0           |NULL

只有第一行有 OnHand 值的原因是因為我可以從另一個表中獲取它,該表存儲任何股票代碼的當前手頭數量.

The reason only the top row has an OnHand value is because I can get this from another table that stores the current qty on hand for any stock code.

表中的其他記錄取自另一個表,該表記錄了任何給定日期的所有移動.

The other records in the table are taken from another table that logs all the movement for any given day.

我想更新上表,以便 OnHand 列根據前一條記錄的庫存和變動顯示該行日期的 QtyOnHand,更新結束時如下所示:

I want to update the above table so that the OnHand column shows the QtyOnHand for that row's date based on the previous record's stock and movement, such that is looks like this at the end of the update:

Date  |StockCode|DaysMovement|OnHand
29-Jul|SC123    |30          |500
28-Jul|SC123    |15          |470
27-Jul|SC123    |0           |455
26-Jul|SC123    |4           |455
25-Jul|SC123    |-2          |451
24-Jul|SC123    |0           |453

我目前正在使用 CURSOR 實現這一目標.但性能真的很糟糕,超過了數千條記錄.

I'm currently achieving this with a CURSOR. But performance really sucks over thousands of records.

是否有一些基于 SET 的 UPDATE 語句可以運行以達到相同的結果?

Is there some SET-based UPDATE statement I can run that will achieve the same result?

推薦答案

試試這個 (小提琴演示)

Try this (Fiddle demo)

DECLARE @Movement INT , @OnHandRunning INT  

;WITH CTE AS
(
    SELECT TOP 100 percent DaysMovement, OnHand 
    FROM Table1
    ORDER BY [StockCode], [Date] DESC
)
UPDATE CTE SET @OnHandRunning = OnHand = COALESCE(@OnHandRunning - @Movement, OnHand),
               @Movement = DaysMovement

更新:對于多個StockCodes,您可以修改上面的查詢,如下所示(小提琴演示 2):

UPDATE: For multiple StockCodes you can modify above query like below (Fiddle demo 2):

DECLARE @Movement INT , @OnHandRunning INT, @StockCode VARCHAR(10) = '' 

;WITH CTE AS
(
    SELECT TOP 100 percent DaysMovement, OnHand, StockCode  
    FROM Table1
    ORDER BY [StockCode],[Date] DESC
)
UPDATE CTE SET @OnHandRunning = OnHand = 
       CASE WHEN @StockCode<> StockCode THEN OnHand ELSE @OnHandRunning - @Movement END,
       @Movement = DaysMovement,
       @StockCode = StockCode

這篇關于具有累積值的 SQL 更新表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 亚洲一区不卡 | 毛片在线免费 | 888久久久 | 久久久久亚洲精品 | 国产精品久久久久永久免费观看 | 日本精品一区二区三区视频 | 欧美一级免费观看 | 五月婷婷丁香婷婷 | 中文字幕视频网 | 一区二区三区欧美 | 精品一区二区三区四区五区 | 欧美99| www天天操 | 国产91精品久久久久久久网曝门 | 午夜视频网站 | 久久久久久久av麻豆果冻 | 自拍偷拍中文字幕 | 久久亚洲一区 | av av在线 | 亚洲精品乱码久久久久久蜜桃91 | 午夜二区| 亚洲午夜av久久乱码 | 国产精品呻吟久久av凹凸 | 婷婷激情五月网 | 欧美国产一区二区 | 欧美日一区| 久久国 | 中文字幕 在线观看 | 91久久久久久久久久久 | 天天操操操操操 | 久久国产精品精品 | 精品国产99| 日韩欧美亚洲一区 | 亚洲成人一级 | 国产欧美三区 | 黑人巨大精品欧美一区二区免费 | 国产a区| 麻豆成人在线视频 | 国产精品成人一区二区三区 | 综合色导航 | 成人久久久|