SQL Server 逐行操作

发布于 2024-09-09 02:28:00 字数 605 浏览 4 评论 0原文

我正在尝试根据“值”列中的前一行记录在“增量”列中执行一些操作,

例如

row_num| Period | Measure | Decay
1      | Jan 08 | 10      | 
2      | Feb 08 | 18      | 
3      | Mar 08 | 7       | 
4      | Apr 08 | 67      | 

我想根据公式更新“衰减”列,

row_num| Period | Measure| Decay
1      | Jan 08 | 10     | = 10             -> first value in 'Measures'
2      | Feb 08 | 18     | = 10*0.5+18 = 23 -> previous decay record *0.5 + current measure
3      | Mar 08 | 7      | = 23*0.5+7  = 18.5
4      | Apr 08 | 67     | = 18.5*0.5+67 = 76.25

光标是否适用于此处?语法会是什么样子? 谢谢

I am trying to do some operation in column 'Increment' based on previous row record in column 'Value'

e.g.

row_num| Period | Measure | Decay
1      | Jan 08 | 10      | 
2      | Feb 08 | 18      | 
3      | Mar 08 | 7       | 
4      | Apr 08 | 67      | 

i would like to update column 'Decay' based on a formula

row_num| Period | Measure| Decay
1      | Jan 08 | 10     | = 10             -> first value in 'Measures'
2      | Feb 08 | 18     | = 10*0.5+18 = 23 -> previous decay record *0.5 + current measure
3      | Mar 08 | 7      | = 23*0.5+7  = 18.5
4      | Apr 08 | 67     | = 18.5*0.5+67 = 76.25

would cursor be applicable here? how would the syntax be like?
thank you

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

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

发布评论

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

评论(2

魂ガ小子 2024-09-16 02:28:00

这是使用递归 CTE 的一个运行示例(另请注意,您的示例不正确):

-- SO3192010

DECLARE @t AS TABLE (row_num int NOT NULL, Period varchar(6) NOT NULL, Measure float NOT NULL)
INSERT INTO @t VALUES (1, 'Jan 08', 10)
    ,(2, 'Feb 08', 18)
    ,(3, 'Mar 08', 7)
    ,(4, 'Apr 08', 67)

;WITH r AS (
    SELECT t.*, Measure AS Decay
    FROM @t AS t
    WHERE t.row_num = 1

    UNION ALL

    SELECT t.*, r.Decay * 0.5 + t.Measure AS Decay
    FROM r
    INNER JOIN @t AS t
        ON t.row_num = r.row_num + 1
)
SELECT *
FROM r
ORDER BY row_num​

Here's a running example using a recursive CTE (also note that the arithmetic in your example is incorrect):

-- SO3192010

DECLARE @t AS TABLE (row_num int NOT NULL, Period varchar(6) NOT NULL, Measure float NOT NULL)
INSERT INTO @t VALUES (1, 'Jan 08', 10)
    ,(2, 'Feb 08', 18)
    ,(3, 'Mar 08', 7)
    ,(4, 'Apr 08', 67)

;WITH r AS (
    SELECT t.*, Measure AS Decay
    FROM @t AS t
    WHERE t.row_num = 1

    UNION ALL

    SELECT t.*, r.Decay * 0.5 + t.Measure AS Decay
    FROM r
    INNER JOIN @t AS t
        ON t.row_num = r.row_num + 1
)
SELECT *
FROM r
ORDER BY row_num​
半暖夏伤 2024-09-16 02:28:00
UPDATE
   tbl_test 
SET
   col_name = 'xyq' 
FROM
   (
   SELECT
      *,
      ROW_NUMBER() OVER (ORDER BY name) AS rno
   FROM
     tbl_test  
   )tbl_test
WHERE
   rno = 3  

--rno is the rownumber that u want to update
UPDATE
   tbl_test 
SET
   col_name = 'xyq' 
FROM
   (
   SELECT
      *,
      ROW_NUMBER() OVER (ORDER BY name) AS rno
   FROM
     tbl_test  
   )tbl_test
WHERE
   rno = 3  

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