SQL用以下行中的值的总和更新行

时间:2016-09-09 09:36:47

标签: sql-server tsql

我正在尝试编写一个脚本,用于更新已从两个股票中心合并的股票级别列表。

我的数据摘录;

MovementId  ChangeDate           StockLevel AdjustmentLevel
1835        2016-01-24 09:48:56  -29        -15
1725        2016-01-22 11:49:31  -138       -10
1715        2016-01-22 11:12:47  -128       -28
1701        2016-01-22 10:48:47  -100       -5
1532        2016-01-20 19:55:28  -14        -14
1520        2016-01-20 18:14:57  -95        -14
1512        2016-01-20 17:55:37  -81        -15
1338        2016-01-19 17:46:22  -66        -7

我正在努力实现;

MovementId  ChangeDate           StockLevel AdjustmentLevel
1835        2016-01-24 09:48:56  -167       -15
1725        2016-01-22 11:49:31  -152       -10
1715        2016-01-22 11:12:47  -142       -28
1701        2016-01-22 10:48:47  -114       -5
1532        2016-01-20 19:55:28  -109       -14
1520        2016-01-20 18:14:57  -95        -14
1512        2016-01-20 17:55:37  -81        -15
1338        2016-01-19 17:46:22  -66        -7

这是我到目前为止所拥有的......

SELECT   
movementid,
changedate,
(Lag(stocklevel, 1, 0)  OVER (PARTITION BY   codeid ORDER BY   changedate)) +AdjustmentLevel   AS NewStockLevel,
adjustmentlevel, 
stocklevel as currentstocklevel
FROM   VET_CodeStockMovement_2
where codeid =14877
order by changedate desc

数据库有大约23000行,我需要针对每个不同的CodeID运行完成的脚本。

2 个答案:

答案 0 :(得分:0)

此脚本将有助于更新数据。

我创建了一个名为SAMPLETABLE的表

   IF OBJECT_ID('SAMPLETABLE') IS NOT NULL 
       DROP TABLE SAMPLETABLE
    GO
    Create TABLE SAMPLETABLE
    (
     MOVEMENTID INT,
     CHANGEDATE DATETIME NOT NULL DEFAULT GETDATE(),
     STOCKLEVEL DECIMAL(5,2),
     ADJUSTMENTLEVEL DECIMAL(5,2)
    )

在OriginalTable SAMPLETABLE中插入您的数据

INSERT INTO SAMPLETABLE VALUES(1033,GETDATE(),-14.00,-14.00)
GO
INSERT INTO SAMPLETABLE VALUES(1036,GETDATE(),0,14.00)
GO
INSERT INTO SAMPLETABLE VALUES(1056,GETDATE(),-5.00,-5.00)
GO
INSERT INTO SAMPLETABLE VALUES(1077,GETDATE(),-15.00,10.00)
GO
INSERT INTO SAMPLETABLE VALUES(1205,GETDATE(),-30.00,-15.00)

我创建了tempTable

  

创建了新的临时表,因为我一直在逐行更新数据。如果你不这样做那将是错误的总数。创建了TempTable并插入原始表数据。

IF OBJECT_ID('tempdb..##TMPSAMPLETABLE') IS NOT NULL 
    DROP TABLE ##TMPSAMPLETABLE
GO
Create TABLE ##TMPSAMPLETABLE
(
 MOVEMENTID INT,
 CHANGEDATE DATETIME NOT NULL DEFAULT GETDATE(),
 STOCKLEVEL DECIMAL(5,2),
 ADJUSTMENTLEVEL DECIMAL(5,2)
)
GO 
INSERT INTO ##TMPSAMPLETABLE SELECT MOVEMENTID,CHANGEDATE , STOCKLEVEL , ADJUSTMENTLEVEL FROM SAMPLETABLE

更新原始数据

  

此脚本根据sum tempTable更新原始文件   ADMUSTMENTLEVEL列由movementid

DECLARE @MOVEMENTID INT
DECLARE @STOCKLEVEL DECIMAL(5,2)
DECLARE @ADJUSTMENTLEVEL DECIMAL(5,2)
DECLARE @SUMVALUE DECIMAL(5,2)

DECLARE MY_CURSOR CURSOR  
FOR 
SELECT MOVEMENTID ,
       STOCKLEVEL ,
       ADJUSTMENTLEVEL
FROM ##TMPSAMPLETABLE ORDER BY MOVEMENTID DESC

OPEN MY_CURSOR
FETCH NEXT FROM MY_CURSOR INTO @MOVEMENTID, @STOCKLEVEL, @ADJUSTMENTLEVEL
WHILE @@FETCH_STATUS = 0
BEGIN 
  SET @SUMVALUE = (SELECT SUM(ADJUSTMENTLEVEL) FROM ##TMPSAMPLETABLE WHERE MOVEMENTID <= @MOVEMENTID);
  UPDATE SAMPLETABLE SET ADJUSTMENTLEVEL = @SUMVALUE WHERE MOVEMENTID = @MOVEMENTID;
    FETCH NEXT FROM MY_CURSOR INTO @MOVEMENTID, @STOCKLEVEL, @ADJUSTMENTLEVEL
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR

结果

SELECT * FROM ##TMPSAMPLETABLE ORDER BY MOVEMENTID DESC

SELECT * FROM SAMPLETABLE ORDER BY MOVEMENTID DESC

删除了TempTable

IF OBJECT_ID('tempdb..##TMPSAMPLETABLE') IS NOT NULL 
    DROP TABLE ##TMPSAMPLETABLE
GO

答案 1 :(得分:0)

我找到了一种使用游标来实现我需要的方法,因为这可以运行以更新所有23000行。虽然它可能需要一段时间才能运行并且不是最资源友好的,但它应该实现我所需要的,而无需为每一行手动输入任何数据/写入更新。

DECLARE @codeid INT

DECLARE CodeCursor CURSOR FOR (SELECT DISTINCT(CodeId) FROM vet_codestockmovement)

OPEN CodeCursor

FETCH NEXT FROM CodeCursor INTO @codeid

WHILE @@FETCH_STATUS = 0
BEGIN

DECLARE @movementid INT

DECLARE StockCursor CURSOR FOR (SELECT movementid FROM vet_codestockmovement WHERE codeid =@codeid)

OPEN StockCursor

FETCH NEXT FROM StockCursor INTO @movementid

WHILE @@FETCH_STATUS = 0
BEGIN

DECLARE @lagvalue INT

SELECT @lagvalue = temp.NewStockLevel  FROM (
SELECT  (LAG(stocklevel, 1, 0)  OVER (PARTITION BY   codeid ORDER BY   changedate)) + AdjustmentLevel AS NewStockLevel, MovementId
FROM   VET_CodeStockMovement
WHERE codeid =@codeid) temp
WHERE temp.MovementId = @movementid

UPDATE dbo.VET_CodeStockMovement SET Stocklevel = @lagvalue WHERE movementid = @movementid

    FETCH NEXT FROM StockCursor INTO @movementid
END

CLOSE StockCursor
DEALLOCATE StockCursor

    FETCH NEXT FROM CodeCursor INTO @codeid
END

CLOSE CodeCursor
DEALLOCATE CodeCursor