【问题标题】:Summing the sum from two seperate tables将两个单独表格的总和相加
【发布时间】:2017-06-12 07:18:28
【问题描述】:

我有以下疑问:

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;

有人可以帮我了解包含决赛桌的语法吗?

谢谢!

【问题讨论】:

  • 为什么不分享你的尝试?
  • 添加了我尝试过的修改!
  • 我无法跟踪您的整个流程(当天太晚了),但是:如果您按帐户和月份分组并选择金额总和,分别在两个行集中(它们是基表, 视图, 子查询, 连接结果等) - 为什么你不能在任何分组依据和总和之前对所有行集、仅相关列进行 UNION ALL,然后应用分组并将函数聚合到 UNION ALL 的结果?这将更简洁(更容易编写和为未来维护代码的开发人员遵循)和更高效。
  • @mathguy。这不是我做的吗? ;)
  • @BobC - 我没有仔细阅读您的解决方案,但我在其中看到,在 UNION ALL 的第一个分支中,有一个“分组依据”和一个 sum(amt_due)。那里需要吗?不能等到UNION ALL之后吗?我不一定说它可以,但这就是我的想法。

标签: sql plsql oracle11g group-by sum


【解决方案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    

【讨论】:

    猜你喜欢
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多