+ -
当前位置:首页 → 问答吧 → 在线等,急.....

在线等,急.....

时间:2011-12-19

来源:互联网

有表:
数据如下:

时间 金额
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的金额
 

作者: song19830708   发布时间: 2011-12-19

用select 语句

作者: song19830708   发布时间: 2011-12-19

SQL code
select sum(金额) over (order by 时间 ) from table_test

作者: xpingping   发布时间: 2011-12-19

sum over()

作者: dws2004   发布时间: 2011-12-19

take it easy.
SQL code
SELECT   tb1.time, SUM (tb1.Amount) Sum_Amount
    FROM   t tb1, t tb2
   WHERE   tb1.time >= tb2.time
GROUP BY   tb1.time

作者: tx2730   发布时间: 2011-12-19

sum over 
或者这样也行
SQL code

select 时间,
(select sum(nvl(金额,0)) from tb_name b where b.时间<=a.时间) as  金额
from tb_name a

作者: lxpbs8851   发布时间: 2011-12-19

按月份进行累加
SQL code

 jmbdat    dayt    y       mon

  27-9月 -07 2033.2 2007 200709

  28-9月 -07 2750.28 2007 200709

  29-9月 -07 2885.68 2007 200709

  30-9月 -07 2556.68 2007 200709

  01-10月-07 2903.04 2007 200710

  02-10月-07 1002.96 2007 200710

  03-10月-07 1038.24 2007 200710


  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

作者: HJ_daxian   发布时间: 2011-12-19

实测数据:
SQL code

CREATE TABLE T66
(
    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;



实测结果:

作者: LuiseRADL   发布时间: 2011-12-19

引用 7 楼 luiseradl 的回复:

实测数据:
SQL code

CREATE TABLE T66
(
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……

+1

作者: huan_lxyd   发布时间: 2011-12-19

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;



作者: zyuc_wangxw   发布时间: 2011-12-19