【问题标题】:SQL ORA-00937: not a single-group group function errorSQL ORA-00937: 不是单组组函数错误
【发布时间】:2019-05-11 00:36:28
【问题描述】:

我很难理解为什么 Oracle 不让我运行它并给我一个“ORA-00937: not a single-group group function”错误。我有我的 GROUP BY 和唯一应该在其中的列。也许我做错了查询?帮助?提前致谢

显示公司名称和股票在全球股东交易总量中最多的公司的交易量。

SELECT MAX(COUNT(trade.shares)) AS "Max Count of Company Stock",
       company.name
FROM   trade
       JOIN company
            ON  company.stock_id = trade.stock_id
GROUP BY company.name;

【问题讨论】:

  • 您在聚合函数中使用聚合函数,会产生此错误。你想完成什么?
  • 交易量是指交易股份,在这种情况下。因此,我试图找到其股票总交易量最大的公司。所以我想计算所有不同的公司交易股份,并使用该信息,我想打印总股份最多的一家公司

标签: sql oracle ora-00937


【解决方案1】:

如果我理解您要完成的工作,以下内容应该可以帮助您:

WITH cteCompany_shares AS (SELECT c.NAME, SUM(t.SHARES) AS SHARES_TRADED
                             FROM COMPANY c
                             INNER JOIN TRADE t
                               ON t.STOCK_ID = c.STOCK_ID
                             GROUP BY c.NAME)
SELECT cs.NAME, cs.SHARES_TRADED
  FROM cteCompany_shares cs
  WHERE cs.SHARES_TRADED = (SELECT MAX(SHARES_TRADED)
                              FROM cteCompany_shares);

【讨论】:

  • 我会尝试一下,如果它有效,请告诉您。谢谢!
【解决方案2】:

首先,我认为您想要sum 股票,而不是count 他们。

问题是您的查询试图一次执行两个级别的聚合(首先对份额进行计数/求和,然后取其中的最大值),这是不可能的。

试试这个:

select c.name
     , count(t.shares) as "Number of trades"
     , sum(t.shares) as "Trade volume"
from   trade t
       join company c on c.stock_id = t.stock_id
group by c.name
order by sum(t.shares) desc
fetch first row only;

fetch first 子句需要 Oracle 12.1 或更高版本。)

或者这个:

select name, total_shares
from   ( select c.name
              , sum(t.shares) as total_shares
              , rank() over (order by sum(t.shares) desc) as ranking
         from   trade t
                join company c on c.stock_id = t.stock_id
         group by c.name )
where  ranking = 1;

样本数据:

create table company
( stock_id  number primary key
, name      varchar2(30) not null );

create table trade
( stock_id  references company
, shares    number not null );

insert all
    into company values (1, 'Apple')
    into company values (2, 'Microsoft')
    into company values (3, 'Oracle')
    into company values (4, 'Huawei')
    into company values (5, 'Robertson Solutions')
select * from dual;

insert all
    into trade values (1,  10)
    into trade values (2,   5)
    into trade values (3, 100)
    into trade values (4, 200)
    into trade values (5,   5)
    into trade values (1,  20)
    into trade values (2,  30)
    into trade values (3,  40)
    into trade values (4,  50)
    into trade values (5,  20)
    into trade values (1,  70)
select * from dual;

汇总数据:

select c.name
     , sum(t.shares) as total_shares
     , rank() over (order by sum(t.shares) desc) as ranking
from   trade t
       join company c  on  c.stock_id = t.stock_id
group by c.name
order by total_shares desc;

NAME                 TOTAL_SHARES    RANKING
-------------------- ------------ ----------
Huawei                        250          1
Oracle                        140          2
Apple                         100          3
Microsoft                      35          4
Robertson Solutions            25          5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 2012-04-16
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多