累计统计

题目1,

表t_transaction:

time          deal
2000-01   10000
2000-02   20000
2000-03   30000
2000-04   40000

 

要求得到如下形式的查询结果
time          balance
2000-01   100000
2000-02   300000
2000-03   600000
2000-04   1000000

可以使用以下sql语句:

 

方法1:
SELECT b.time, SUM (a.deal) balance
FROM t_transaction a, t_transaction b
WHERE a.time<= b.time
GROUP BY b.time

 

方法2:
select t.time, sum(t.deal) over(partition BY t.time) as balance
from t_transaction t

 

方法3:分子等差数列。
select row_number() over(order by time) as rank,
time,
deal * ((row_number() over(order by time)) +1)/2 as deal,
from  t_transaction

 

 

你可能感兴趣的:(sql)