SQLite select 内部更新

SQLite select inside of update

所以有 2 个 table,Transactionscreated_at 列,Transaction_associationsamountremaining_balance 列,等等.显然,我需要在 amount 列上计算 运行 sum(total),按 created_at 列排序。唯一的问题是,我需要获取在 正在计算的当前交易之前 创建的所有交易的 SUM。我需要更新查询中的 select 才能 SELECTcurrent_transactions table 才能掌握当前的 created_at 日期。然而我不能。我错过了什么吗?这种方法有替代方法吗?

UPDATE Transaction_associations SET remaining_balance = 
( 
    SELECT SUM (Transaction_associations.amount) 
    FROM Transactions 
    JOIN Transaction_associations ON Transactions.id = transaction_id 
    WHERE created_at <= current_transactions.created_at // here
) 
WHERE id IN
(
    SELECT id 
    FROM Transaction_associations 
    JOIN Transactions ON Transactions.id = transaction_id 
    WHERE created_at >= '2014-11-24'
)

编辑: 添加了示例。

Transactions     Transaction_associations
created_at       amount     remaining_balance
2014-02-01       100        100
2014-03-01        50        150
2014-04-01       205        355

后期编辑: 添加了用于 SQLFiddle 的完整代码。我在 SUM 上用 TA2 替换了 Transaction_associations,因为它抱怨 misuse of aggregate: SUM()

DROP TABLE IF EXISTS Transactions;
DROP TABLE IF EXISTS Transaction_associations;
CREATE TABLE Transactions ( id integer,  created_at text);
CREATE TABLE Transaction_associations ( id integer,  amount integer, remaining_balance integer, transaction_id integer);

  INSERT INTO Transactions VALUES (1,'2015');
  INSERT INTO Transactions VALUES (2,'2014');
  INSERT INTO Transactions VALUES (3,'2013');
  INSERT INTO Transactions VALUES (4,'2012');
  INSERT INTO Transactions VALUES (5,'2010');
  INSERT INTO Transaction_associations VALUES (6, 100, 0, 1);
  INSERT INTO Transaction_associations VALUES (7, 20, 0, 2);
  INSERT INTO Transaction_associations VALUES (8, 3, 0, 3);
  INSERT INTO Transaction_associations VALUES (9, 40, 0, 4);
  INSERT INTO Transaction_associations VALUES (10, 500, 0, 5);



UPDATE Transaction_associations
SET remaining_balance =
(
    SELECT SUM(TA2.amount) 
    FROM Transactions 
    JOIN Transaction_associations AS TA2 ON Transactions.id = TA2.transaction_id
    WHERE created_at <= (SELECT created_at
                         FROM Transactions
                         WHERE id = TA2.transaction_id)
) 
WHERE transaction_id IN
(
    SELECT id
    FROM Transactions
    WHERE created_at >= '2013'
);

SELECT * from Transactions join Transaction_associations on Transactions.id = Transaction_associations.transaction_id;

结果是,这是错误的:

1   2015    6   100 663 1
2   2014    7   20  663 2
3   2013    8   3   663 3
4   2012    9   40  0   4
5   2010    10  500 0   5

结果应该是:

1   2015    6   100 663 1
2   2014    7   20  563 2
3   2013    8   3   543 3
4   2012    9   40  0   4
5   2010    10  500 0   5

要多次使用相同的 table 名称,请重命名其中一个。这对于 UPDATE 是不可能的,因此您必须在 SELECT 中执行此操作。 要查找相应的时间戳,请使用另一个子查询。 再加上一些简化,这就变成了:

UPDATE Transaction_associations
SET remaining_balance =
(
    SELECT SUM(TA2.amount) 
    FROM Transactions 
    JOIN Transaction_associations AS TA2 ON Transactions.id = TA2.transaction_id
    WHERE created_at <= (SELECT created_at
                         FROM Transactions
                         WHERE id = Transaction_associations.transaction_id)
) 
WHERE transaction_id IN
(
    SELECT id
    FROM Transactions
    WHERE created_at >= '2014-11-24'
);