簡體   English   中英

按表中的分組記錄運行總計

[英]Running total by grouped records in table

我有這樣的表(Oracle,10)

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

我需要的是按帳戶asc和Bookdate asc按帳戶順序分組的新表格,其中包含一個正在運行的總字段,如下所示:

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;

編輯:

忘了加; 你指定你想要對表進行排序 - 這實際上沒有意義,並且讓我認為你真的意味着你想要查詢/視圖 - 排序是你執行的查詢的結果,而不是那些在表(忽略索引組織表等)。

我將從這個非常重要的問題開始:不要創建一個表來保存這些數據。 當你這樣做時,你會發現你需要保持它,這將成為一個永無止境的頭痛。 如果要執行此操作,請編寫視圖以返回額外列。 如果你有一個數據倉庫的工作,那么也許你會做這樣的事情,但即使如此,寧可視圖的一側,除非你根本無法得到你需要的索引,不俗的硬件等性能

這是一個以您需要的方式返回行的查詢。

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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM