如何解决ORA-00937:计算百分比时不是单组组功能?

Lia*_*iao 6 sql oracle ora-00937

我正在尝试获取某个区域中可用的itemid的百分比.使用我的查询,我收到一个错误ORA-00937: not a single-group group function

所有细节:

我有这两个表:

ALLITEMS
---------------
ItemId  | Areas
---------------
1       | EAST
2       | EAST
3       | SOUTH
4       | WEST

CURRENTITEMS
---------------
ItemId
---------------
1
2
3
Run Code Online (Sandbox Code Playgroud)

并希望这个结果:

---------------
Areas| Percentage
---------------
EAST   | 50  --because ItemId 1 and 2 are in currentitems, so 2 items divided by the total 4 in allitems = .5
SOUTH  | 25   --because there is 1 item in currentitems table that are in area SOUTH (so 1/4=.25)
WEST   | 0  --because there are no items in currentitems that are in area WEST
Run Code Online (Sandbox Code Playgroud)

DDL:

drop table allitems;
drop table currentitems;

Create Table Allitems(ItemId Int,areas Varchar2(20));
Create Table Currentitems(ItemId Int);
Insert Into Allitems(Itemid,Areas) Values(1,'east');
Insert Into Allitems(ItemId,areas) Values(2,'east');
insert into allitems(ItemId,areas) values(3,'south');
insert into allitems(ItemId,areas) values(4,'east');

Insert Into Currentitems(ItemId) Values(1);
Insert Into Currentitems(ItemId) Values(2);
Insert Into Currentitems(ItemId) Values(3);
Run Code Online (Sandbox Code Playgroud)

我的查询:

Select  
areas,
(
Select 
Count(Currentitems.ItemId)*100 / (Select Count(ItemId) From allitems inner_allitems Where inner_allitems.areas = outer_allitems.areas )
From 
Allitems Inner_Allitems Left Join Currentitems On (Currentitems.Itemid = Inner_Allitems.Itemid) 
Where inner_allitems.areas = outer_allitems.areas
***group by inner_allitems.areas***
***it worked by adding the above group by***
) "Percentage Result"
From 
allitems outer_allitems
Group By 
areas
Run Code Online (Sandbox Code Playgroud)

错误:

Error at Command Line:81 Column:41 (which is the part `(Select Count(ItemId) From allitems inner_allitems Where inner_allitems.areas = outer_allitems.areas )`)
Error report:
SQL Error: ORA-00937: not a single-group group function
Run Code Online (Sandbox Code Playgroud)

当我在SQL Server中运行完全相同的查询时,它工作正常.我如何在Oracle中解决这个问题?

Jef*_*emp 9

分析是你的朋友:

SELECT DISTINCT
       areas
      ,COUNT(currentitems.itemid)
       OVER (PARTITION BY areas) * 100
       / COUNT(*) OVER () Percentage
FROM allitems, currentitems
WHERE allitems.itemid = currentitems.itemid(+);
Run Code Online (Sandbox Code Playgroud)

  • 便携性可怜:) (6认同)
  • 他提到了 SQL Server 和 Oracle,也许查询必须是可移植的,即与供应商无关。 (2认同)

APC*_*APC 5

只是为了好玩,这是一种无需分析的方法。

Jeffrey 的解决方案需要一个 DISTINCT,因为areas. 该表实际上是和 假定表allitems之间的交集表。在以下查询中,这由内联视图表示。还有另一个内联视图,它为我们提供了 中的记录总数。该计数必须包含在 GROUP BY 子句中,因为它不是聚合投影仪。currentitemsareasaitotallitems

SQL> select ai.areas
  2         , (count(currentitems.itemid)/tot.cnt) * 100 as "%"
  3  from
  4      ( select count(*) as cnt from allitems ) tot
  5      , ( select distinct areas as areas from allitems ) ai
  6      , currentitems
  7      , allitems
  8  where allitems.areas = ai.areas
  9  and   allitems.itemid = currentitems.itemid(+)
 10  group by ai.areas, tot.cnt
 11  /

AREAS                         %
-------------------- ----------
east                         50
south                        25
west                          0

SQL>
Run Code Online (Sandbox Code Playgroud)

我不知道这种方法是否会比杰弗里的解决方案表现得更好:它很可能会表现得更差(分析查询的一致获取肯定更少)。它很有趣,因为它更清楚地突出了问题。