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

发布于 2024-09-14 04:54:17 字数 814 浏览 4 评论 0原文

这个让我卡住了。

数据:

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 语句更新预测列,如下所示:

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。

非常感谢。

this one has me stuck fast.

Data:

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 

Now what I'm trying to do is loop through the current contents of the table and update the Forecast column using conditional IF statements as follows:

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

I did think of using the CASE statement though calculating the forecast using previous dates stumped me, I presume I will need to utilise a while statements and RBAR.

Much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抹茶夏天i‖ 2024-09-21 04:54:17

试试这个:

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)

try this:

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

OUTPUT:

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)
小忆控 2024-09-21 04:54:17

这个怎么样:

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]

How about this:

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]
你是我的挚爱i 2024-09-21 04:54:17

您可以创建一个奇特的组合语句,但我只使用 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

You could make one fancy combined statement, but I'd just use 3 separate update statements:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文