答案 0 :(得分:0)
您可以使用相关子查询执行此操作:
update t
set b = (select sum(t2.b) from t t2 where t2.A <= t.A);
这是标准SQL,可以在Oracle或Teradata中使用。
答案 1 :(得分:0)
让我们稍微复杂一点的例子(A
中的更改与CREATE TABLE ABCD( A, B ) AS
SELECT 1, 0 FROM DUAL UNION ALL
SELECT 1, 0 FROM DUAL UNION ALL
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 2, 1 FROM DUAL UNION ALL
SELECT 2, 0 FROM DUAL UNION ALL
SELECT 3, 0 FROM DUAL UNION ALL
SELECT 3, 1 FROM DUAL UNION ALL
SELECT 3, 0 FROM DUAL;
中的更改无关):
Oracle安装程序:
UPDATE ABCD t1
SET b = (
SELECT sm
FROM (
SELECT ROWID AS id,
SUM( b ) OVER ( ORDER BY a, ROWNUM ) AS sm
FROM ABCD
) t2
WHERE t1.ROWID = t2.ID
);
<强>更新强>:
SELECT * FROM ABCD;
A B
- -
1 0
1 0
1 1
2 2
2 2
3 2
3 3
3 3
<强>输出强>:
[![title](thumbnail_url?height=240&width=320)](url)
(注意:这是一个Oracle解决方案;我不知道它是否会在Teradata中运行。)