从两个单独的表中求和

时间:2017-01-26 17:57:01

标签: sql plsql oracle11g group-by sum

我有以下查询:

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
---------- | --------- | ---------

会给出结果:

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

我现在想要添加一个额外的表格(tb4)并按照我们上面的说法对收费进行求和。请考虑新表中的这些行(tb4)。

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

总结收费会给我们带来结果:

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

现在我想将第一个查询的结果与第二个查询的结果相加。所以:

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

总结

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

给出最终结果:

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;

有人可以帮助我使用包含最终表格的语法吗?

谢谢!

1 个答案:

答案 0 :(得分: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