有表:
数据如下:时间 金额
01-12月-11 12.00.00.000000 上午 -2168633.15
02-12月-11 12.00.00.000000 上午 -2055022.38
03-12月-11 12.00.00.000000 上午 -1154342.89
06-12月-11 12.00.00.000000 上午 -2169146.53
07-12月-11 12.00.00.000000 上午 -4260406.83
08-12月-11 12.00.00.000000 上午 -2065691.07
09-12月-11 12.00.00.000000 上午 -2208221.85
10-12月-11 12.00.00.000000 上午 -1528451.94怎样根据上表将数据构成另一张表?
要求:
金额: 金额 + 当前记录时间前的金额和
如:2011-12-03 = 2011-12-03 + 2011-12-02 + 2011-12-01的金额
数据如下:时间 金额
01-12月-11 12.00.00.000000 上午 -2168633.15
02-12月-11 12.00.00.000000 上午 -2055022.38
03-12月-11 12.00.00.000000 上午 -1154342.89
06-12月-11 12.00.00.000000 上午 -2169146.53
07-12月-11 12.00.00.000000 上午 -4260406.83
08-12月-11 12.00.00.000000 上午 -2065691.07
09-12月-11 12.00.00.000000 上午 -2208221.85
10-12月-11 12.00.00.000000 上午 -1528451.94怎样根据上表将数据构成另一张表?
要求:
金额: 金额 + 当前记录时间前的金额和
如:2011-12-03 = 2011-12-03 + 2011-12-02 + 2011-12-01的金额
SELECT tb1.time, SUM (tb1.Amount) Sum_Amount
FROM t tb1, t tb2
WHERE tb1.time >= tb2.time
GROUP BY tb1.time
或者这样也行select 时间,
(select sum(nvl(金额,0)) from tb_name b where b.时间<=a.时间) as 金额
from tb_name a
select tt.*, sum(tt.dayt) over (partition by tt.mon order by tt.jmbdat,tt.y,tt.mon) as sum_dayt from tb1 tt;
Result : JMBDAT DAYT Y MON SUM_DAYT ----------- ---------- ---------- ------ ---------- 2007-9-27 2033.2 2007 200709 2033.2 2007-9-28 2750.28 2007 200709 4783.48 2007-9-29 2885.68 2007 200709 7669.16 2007-9-30 2556.68 2007 200709 10225.84 2007-10-1 2903.04 2007 200710 2903.04 2007-10-2 1002.96 2007 200710 3906 2007-10-7 1038.24 2007 200710 4944.24
(
MyTime DATE,
Money NUMBER(4)
);
INSERT INTO T66 VALUES(to_date('2011-12-01', 'YYYY-MM-DD'), 1);
INSERT INTO T66 VALUES(to_date('2011-12-02', 'YYYY-MM-DD'), 2);
INSERT INTO T66 VALUES(to_date('2011-12-03', 'YYYY-MM-DD'), 3);
INSERT INTO T66 VALUES(to_date('2011-12-04', 'YYYY-MM-DD'), 4);
INSERT INTO T66 VALUES(to_date('2011-12-05', 'YYYY-MM-DD'), 5);SELECT MyTime,
SUM(SUM(Money)) OVER(ORDER BY MyTime ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
AS 累积和
FROM T66
GROUP BY MyTime
ORDER BY MyTime;
实测结果:
SUM(SUM(Money)) OVER(ORDER BY MyTime ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
AS 累积和
FROM T66
GROUP BY MyTime
ORDER BY MyTime;