使用IF语句条件更新表中的每一行

时间:2010-08-16 12:44:34

标签: sql tsql if-statement while-loop sql-update

这个让我陷入困境。

数据:

Month           Total         Impact    Forecast
------------------------------------------------
2010-04-01    45792.0000    1.0000    NULL
2010-05-01    51789.0000    1.0000    NULL
2010-06-01    58228.0000    1.0000    NULL
2010-07-01    52956.5217    1.0000    NULL
2010-08-01    53600.4700    0.8810    NULL
2010-09-01    54257.8784    1.1838    NULL
2010-10-01    55134.0669    1.0000    NULL 

现在我要做的是循环遍历表的当前内容并使用条件IF语句更新Forecast列,如下所示:

If Impact = 1.0000 then forecast = (current month) total * Impact
If Impact < 1.0000 then forecast = (month -1) total * Impact
If Impact > 1.0000 then forecast = (month -2) total * Impact

我确实想过使用CASE语句虽然使用之前的日期计算预测让我感到难过,但我认为我需要使用while语句和RBAR。

非常感谢。

3 个答案:

答案 0 :(得分:3)

试试这个:

DECLARE @YourTable table (MonthOf datetime, TotalOf numeric(9,4), Impact numeric(9,4), forecast numeric(9,4))

INSERT @YourTable VALUES('2010-04-01',    45792.0000,    1.0000,    NULL)
INSERT @YourTable VALUES('2010-05-01',    51789.0000,    1.0000,    NULL)
INSERT @YourTable VALUES('2010-06-01',    58228.0000,    1.0000,    NULL)
INSERT @YourTable VALUES('2010-07-01',    52956.5217,    1.0000,    NULL)
INSERT @YourTable VALUES('2010-08-01',    53600.4700,    0.8810,    NULL)
INSERT @YourTable VALUES('2010-09-01',    54257.8784,    1.1838,    NULL)
INSERT @YourTable VALUES('2010-10-01',    55134.0669,    1.0000,    NULL)

;WITH MonthValues AS
(
    SELECT
        DATEADD(month,DATEDIFF(month,0,MonthOf),0) AS MonthYear
            ,SUM(TotalOf) TotalOf
    FROM @YourTable
    GROUP BY DATEADD(month,DATEDIFF(month,0,MonthOf),0)
)
UPDATE y
    SET Forecast=CASE
                     WHEN Impact = 1.0000 then m1.TotalOf * Impact
                     WHEN Impact < 1.0000 then m2.TotalOf * Impact
                     WHEN Impact > 1.0000 then m3.TotalOf * Impact
                 END
    FROM @YourTable                     y
        LEFT OUTER JOIN MonthValues  m1 ON DATEADD(month,DATEDIFF(month,0,y.MonthOf),0)=m1.MonthYear
        LEFT OUTER JOIN MonthValues  m2 ON DATEADD(month,-1,DATEADD(month,DATEDIFF(month,0,y.MonthOf),0))=m2.MonthYear
        LEFT OUTER JOIN MonthValues  m3 ON DATEADD(month,-2,DATEADD(month,DATEDIFF(month,0,y.MonthOf),0))=m3.MonthYear

SELECT * FROM @YourTable

输出:

MonthOf                 TotalOf     Impact  forecast
----------------------- ----------- ------- -----------
2010-04-01 00:00:00.000 45792.0000  1.0000  45792.0000
2010-05-01 00:00:00.000 51789.0000  1.0000  51789.0000
2010-06-01 00:00:00.000 58228.0000  1.0000  58228.0000
2010-07-01 00:00:00.000 52956.5217  1.0000  52956.5217
2010-08-01 00:00:00.000 53600.4700  0.8810  46654.6956
2010-09-01 00:00:00.000 54257.8784  1.1838  62689.9304
2010-10-01 00:00:00.000 55134.0669  1.0000  55134.0669

(7 row(s) affected)

答案 1 :(得分:1)

你可以制作一个花哨的组合语句,但我只使用3个单独的更新语句:

UPDATE table
SET forecast = (current month) total * Impact
WHERE Impact = 1.0000
GO
UPDATE table
SET forecast = (month -1) total * Impact 
WHERE Impact < 1.0000
GO
UPDATE table
SET forecast = (month -2) total * Impact 
WHERE Impact > 1.0000
GO

答案 2 :(得分:1)

这个怎么样:

UPDATE [Table]
SET
    [Forecast] = [Impact] * CASE
        WHEN [Impact] = 1.0000 THEN
        (
            SELECT
                [InnerTable].[Total]
            FROM [Table] AS [InnerTable]
            WHERE [InnerTable].[Month] = [Table].[Month]
        )
        WHEN [Impact] < 1.0000 THEN
        (
            SELECT
                [InnerTable].[Total]
            FROM [Table] AS [InnerTable]
            WHERE [InnerTable].[Month] = DATEADD(month, -1, [Table].[Month])
        )
        ELSE
        (
            SELECT
                [InnerTable].[Total]
            FROM [Table] AS [InnerTable]
            WHERE [InnerTable].[Month] = DATEADD(month, -2, [Table].[Month])
        )
    END    
FROM [Table]