按表中的分组记录运行总计

Zso*_*kai 10 sql oracle grouping sum running-total

我有这样的表(Oracle,10)

Account     Bookdate     Amount
      1     20080101        100
      1     20080102        101
      2     20080102        200
      1     20080103       -200
...
Run Code Online (Sandbox Code Playgroud)

我需要的是按帐户asc和Bookdate asc按帐户顺序分组的新表格,其中包含一个正在运行的总字段,如下所示:

Account     Bookdate     Amount     Running_total
      1     20080101        100               100
      1     20080102        101               201
      1     20080103       -200                 1
      2     20080102        200               200
...
Run Code Online (Sandbox Code Playgroud)

有一个简单的方法吗?

提前致谢.

小智 15

你真的需要额外的桌子吗?

您可以使用简单的查询获得所需的数据,如果您希望它看起来像表格,您可以将其显然创建为视图.

这将为您提供您正在寻找的数据:

select 
    account, bookdate, amount, 
    sum(amount) over (partition by account order by bookdate) running_total
from t
/
Run Code Online (Sandbox Code Playgroud)

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

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

如果你真的需要这张桌子,你的意思是你需要不断更新吗?还是只有一次?显然,如果它是一个一个,你可以使用上面的查询"创建表作为选择".

我使用的测试数据是:

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;
Run Code Online (Sandbox Code Playgroud)

编辑:

忘了加; 你指定你想要对表进行排序 - 这实际上没有意义,并且让我认为你真的意味着你想要查询/视图 - 排序是你执行的查询的结果,而不是那些在表(忽略索引组织表等).


tui*_*oel 6

使用分析,就像你的上一个问题一样:

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
/
Run Code Online (Sandbox Code Playgroud)

输出:

   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
Run Code Online (Sandbox Code Playgroud)


Tom*_*m H 5

我将从这个非常重要的警告开始:不要创建一个表来保存这些数据。当你这样做时,你会发现你需要维护它,这将成为一个永无休止的头痛。如果您想这样做,请编写一个视图来返回额外的列。如果您正在使用数据仓库,那么也许您会做类似的事情,但即使如此,也会在视图方面犯错误,除非您根本无法通过索引、像样的硬件等获得所需的性能。

这是一个查询,它将按照您需要的方式返回行。

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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

测试它们的性能,看看哪个更适合您。另外,除了您提供的示例之外,我还没有对它们进行彻底的测试,因此请务必测试一些边缘情况。