简体   繁体   English

SQL:不是单组组函数Oracle SQL

[英]SQL: not a single-group group function Oracle SQL

I have a table like: 我有一张像这样的桌子:

 A   B   C
101  1  100
101  1  110
101  2  200
202  2  300
202  1  100
303  1  200
303  1  300

I am trying to get result like: 我正在尝试获得如下结果:

 A   B   C
101  1  210
202  2  300
303  1  500

i tried running this query like: 我试过像这样运行此查询:

select A, B, max(sum(C))
from table
group by A,B;

But still getting error. 但是仍然出现错误。

ORA-00937: not a single-group group function
00937. 00000 -  "not a single-group group function"

Can anyone help me out. 谁能帮我吗。

Can be done in several ways. 可以通过几种方式完成。 This is using a cte (common table expression). 这使用cte(公用表表达式)。 The cte does the basic group by, ie returns each a/b combination's sum of c. cte进行基本分组,即返回每个a / b组合的c之和。

Then join the cte with a derived table which finds each a's highest sum if c. 然后将cte与一个派生表连接,该表找到每个c的最高和(如果c)。

with cte as
(
    select A, B, sum(C) sumc
    from table
    group by A,B
)
select t1.a, t1.b, t1.sumc
from cte t1
  join (select a, max(sumc) maxc from cte
        group by a) t2 on t1.a = t2.a and t1.sumc = t2.maxc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM