2017-01-26 8 views
0

からの和を合計:は、私は次のクエリを持つ2つの別々のテーブル

select tb1.accountnum, to_char(tb3.month, 'MON, YYYY'), 
      sum(tb3.amt_due) 
from  db_table1 tb1, db_table2 tb2, db_table3 tb3 
where  tb1.acctnum = tb2.acctnum and 
      tb2.acctcode = tb3.acctcode and 
      tb3.type_id = 10   and 
      tb1.status = 'YES' 
group by tb1.accountnum, (tb3.month, 'MON, YYYY'), 
having sum(tb3.amt_due) < 0; 

このクエリは、別途、各月の期限金額を合計し、負の場合は、口座番号を返します。たとえば:

accountnum | Month | Amt_Due 
---------- | --------- | --------- 
     1 | Jan | 15 
---------- | --------- | --------- 
     1 | Jan | -20 
---------- | --------- | --------- 
     1 | Mar |  3 
---------- | --------- | --------- 
     2 | Aug | 13 
---------- | --------- | --------- 
     2 | Dec | -25 
---------- | --------- | --------- 
     2 | Dec | 40 
---------- | --------- | --------- 

は結果が得られます:

私は今、追加のテーブル(TB4)を追加し、私達はちょうど上記したよう電荷を合計する
accountnum | Month | Amt_Due 
---------- | --------- | --------- 
     1 | Jan | -5 
---------- | --------- | --------- 

。新しいテーブルのこれらの行を考慮してください(tb4)。料金を合計

accountnum | Month | misc_charge 
---------- | --------- | --------- 
     1 | Jan | -45 
---------- | --------- | --------- 
     1 | Jan |  25 
---------- | --------- | --------- 
     2 | Sep |  -7 
---------- | --------- | --------- 

は私達に結果を与えるだろう:

accountnum | Month | misc_charge 
---------- | --------- | --------- 
     1 | Jan | -20 
---------- | --------- | --------- 
     2 | Sep |  -7 
---------- | --------- | --------- 

は今、私は2番目の結果に私たちの最初のクエリの結果を合計します。だから、:

accountnum | Month | misc_charge 
---------- | --------- | --------- 
     1 | Jan | -20 
---------- | --------- | --------- 
     2 | Sep |  -7 
---------- | --------- | --------- 

と合計

accountnum | Month | Amt_Due 
---------- | --------- | --------- 
     1 | Jan | -5 
---------- | --------- | --------- 

は、最終的な結果与える:

accountnum | Month | sum(sum(tb3.amt_due) + sum(tb4.misc_charge)) 
---------- | --------- | --------- 
     1 | Jan | -25 
---------- | --------- | --------- 
     2 | Sep |  -7 
---------- | --------- | --------- 

を私はTB4を含めるように私の元のクエリを変更しますが、Oracleは私にエラーを与える: ORA -00935:グループ関数がネストされています。

select tb1.accountnum, to_char(tb3.month, 'MON, YYYY'), 
      to_char(tb4.month, 'MON, YYY'), 
      sum(sum(tb3.amt_due) + sum(tb4.misc_charge)) 
from  db_table1 tb1, db_table2 tb2, db_table3 tb3, db_table4 tb4 
where  tb1.acctnum = tb2.acctnum and 
      tb2.acctcode = tb3.acctcode and 
      tb3.acctcode = tb4.acctcode and 
      tb3.type_id = 10   and 
      tb1.status = 'YES' 
group by tb1.accountnum, to_char(tb3.month, 'MON, YYYY'), 
      to_char(tb4.month, 'MON, YYY') 
having sum(sum(tb3.amt_due) + sum(tb4.misc_charge)) < 0; 

最終的なテーブルを含める構文を教えてもらえますか?

ありがとうございました!

+0

あなたが試したことをなぜ共有しないのですか? – BobC

+0

私が試した修正を加えました! – ComputersAreNeat

+0

私はあなたのプロセス全体を追跡することはできませんが(遅すぎます)、アカウントと月ごとにグループ化し、金額の合計を2つの行セットで別々に選択すると(ベーステーブル、ビュー、サブクエリ、結果UNION ALLの結果にグループ化と集約関数を適用する理由は何ですか?これは、コードを管理している将来の開発者の方がより簡単で(より簡単に書いたりフォローしたりできる)、より効率的です。 – mathguy

答えて

1

私はこのようなものが必要だと思います。私はあなたのデータ/テーブルでそれをテストしていないので、これが動作するかどうか見てください。

select accountnum, month, sum(amt)               
from                       
(                       
    select tb1.accountnum, to_char(tb3.month, 'MON, YYYY') month,       
      sum(tb3.amt_due) amt                
    from  db_table1 tb1, db_table2 tb2, db_table3 tb3          
    where  tb1.acctnum = tb2.acctnum and             
      tb2.acctcode = tb3.acctcode and             
      tb3.type_id = 10   and             
      tb1.status = 'YES'                
    group by tb1.accountnum, (tb3.month, 'MON, YYYY')           
    union all                     
    select tb4.accountnum, to_char(tb4.month, 'MON, YYYY'),          
      sum(tb4.misc_charge)                
    from tb4 
    group by tb4.accountnum, to_char(tb4.month, 'MON, YYYY')                     
)                        
group by accountnum, month                 
having sum(amt) < 0  
関連する問題