我有这样的表(甲骨文,10)

Account     Bookdate     Amount
      1     20080101        100
      1     20080102        101
      2     20080102        200
      1     20080103       -200
...

我需要的是新的分组表按科目顺序按帐户ASC和ASC Bookdate与正在运行的总场,像这样的:

Account     Bookdate     Amount     Running_total
      1     20080101        100               100
      1     20080102        101               201
      1     20080103       -200                 1
      2     20080102        200               200
...

有一个简单的办法做到这一点?

预先感谢。

有帮助吗?

解决方案

你真的需要额外的表?

您可以得到你需要用一个简单的查询,而如果你想让它看起来像一个表可以很明显的创建作为视图的数据。

这将让你你正在寻找的数据:

select 
    account, bookdate, amount, 
    sum(amount) over (partition by account order by bookdate) running_total
from t
/

这将创建一个视图向您显示数据,好像它是一个表:

create or replace view t2
as
select 
    account, bookdate, amount, 
    sum(amount) over (partition by account order by bookdate) running_total 
from t
/

如果你确实需要的表格,你的意思是,你需要不断的更新?或只是一次性的?显然,如果它是一次性的,你可以只是“创建表作为选择”使用上面的查询。

我使用的测试数据是:

create table t(account number, bookdate date, amount number);

insert into t(account, bookdate, amount) values (1, to_date('20080101', 'yyyymmdd'), 100);

insert into t(account, bookdate, amount) values (1, to_date('20080102', 'yyyymmdd'), 101);

insert into t(account, bookdate, amount) values (1, to_date('20080103', 'yyyymmdd'), -200);

insert into t(account, bookdate, amount) values (2, to_date('20080102', 'yyyymmdd'), 200);

commit;

编辑:

忘了补充;你指定你想要的表进行排序 - 这并没有真正意义,并让我觉得你真的是你想要的查询/视图 - 排序是你执行这inherant查询,不是的结果表(忽略索引组织表等)。

其他提示

我会用这个非常重要的caveate开始:不创建一个表来保存这些数据。当你做,你会发现,你需要保持它,这将成为一个永不落幕的头痛。写视图返回额外的列,如果你想这样做。如果你有一个数据仓库的工作和也许你会做这样的事情,但即使如此,宁可视图的一侧,除非你根本无法得到你需要使用索引的性能,体面硬件等

下面是一个查询,将返回行,你需要它们的方式。

SELECT
    Account,
    Bookdate,
    Amount,
    (
        SELECT SUM(Amount)
        FROM My_Table T2
        WHERE T2.Account = T1.Account
          AND T2.Bookdate <= T1.Bookdate
    ) AS Running_Total
FROM
    My_Table T1

另一种可能的解决方案是:

SELECT
    T1.Account,
    T1.Bookdate,
    T1.Amount,
    SUM(T2.Amount)
FROM
    My_Table T1
LEFT OUTER JOIN My_Table T2 ON
    T2.Account = T1.Account AND
    T2.Bookdate <= T1.Bookdate
GROUP BY
    T1.Account,
    T1.Bookdate,
    T1.Amount

测试他们俩性能,看哪个适合你更好。此外,我还没有彻底测试它们超出了你给的例子,所以一定要测试一些边缘情况。

使用分析,就像在你的最后一个问题:

create table accounts
( account number(10)
, bookdate date 
, amount   number(10)
);

delete accounts;

insert into accounts values (1,to_date('20080101','yyyymmdd'),100);
insert into accounts values (1,to_date('20080102','yyyymmdd'),101);
insert into accounts values (2,to_date('20080102','yyyymmdd'),200);
insert into accounts values (1,to_date('20080103','yyyymmdd'),-200);

commit;

select account
,      bookdate 
,      amount
,      sum(amount) over (partition by account order by bookdate asc) running_total
from accounts
order by account,bookdate asc
/

输出:

   ACCOUNT BOOKDATE     AMOUNT RUNNING_TOTAL
---------- -------- ---------- -------------
         1 01-01-08        100           100
         1 02-01-08        101           201
         1 03-01-08       -200             1
         2 02-01-08        200           200
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top