1

我使用了一个类似的查询

SELECT app_id, SUM(amount), date 
FROM abc 
GROUP BY app_id,date

结果显示如下

1   10   1-2-2000
2   20   1-2-2000
3   30   1-2-2000
1   20   2-2-2000
2   40   2-2-2000
3   50   2-2-2000

我想通过新列获得所需的结果

1   10   1-2-2000
2   30   1-2-2000   # sum of amount of app_id=1 and app_id=2
3   60   1-2-2000   # sum of amount of app_id=1 and app_id=2 and app_id=3
1   20   2-2-2000
2   60   2-2-2000   # sum of amount of app_id=1 and app_id=2
3  110   2-2-2000   # sum of amount of app_id=1 and app_id=2 and app_id=3
4

1 回答 1

0

尝试这个:

SELECT a.app_id, (SELECT SUM(amount) FROM abc b WHERE a.date = b.date AND b.app_id <= a.app_id) amount, a.date 
FROM abc a;
于 2012-11-05T12:48:51.747 回答