SQLite选择更新内部

时间:2016-01-16 20:08:25

标签: sqlite android-sqlite

因此,有2个表格,Transactions包含created_at列,Transaction_associations包含amountremaining_balance列。我需要计算amount列上的运行总和(总计),显然按created_at列排序。唯一的问题是我需要获取在当前正在计算的事务之前创建的所有事务的SUM。为了获得当前SELECT日期,我需要在更新查询中选择current_transactions 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

1 个答案:

答案 0 :(得分:1)

要多次使用相同的表名,请重命名其中一个。 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'
);