8

特定の地域で利用可能な itemid のパーセンテージを取得しようとしています。私のクエリを使用すると、エラーが発生しますORA-00937: not a single-group group function

すべての詳細:

私はこれらの2つのテーブルを持っています:

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

CURRENTITEMS
---------------
ItemId
---------------
1
2
3

そしてこの結果が欲しい:

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

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

私のクエリ:

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

エラー:

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

SQL Server でまったく同じクエリを実行すると、正常に動作します。Oracleでこれを修正するにはどうすればよいですか?

4

7 に答える 7

11

分析はあなたの友達です:

SELECT DISTINCT
       areas
      ,COUNT(currentitems.itemid)
       OVER (PARTITION BY areas) * 100
       / COUNT(*) OVER () Percentage
FROM allitems, currentitems
WHERE allitems.itemid = currentitems.itemid(+);
于 2010-01-28T05:09:15.317 に答える
4

なんというか、分析なしでそれを行う方法です。

Jeffrey のソリューションでは、 が重複しているため、DISTINCT が必要でしたareas。このallitemsテーブルは、実際にはcurrentitemsと推定areasテーブルの間の交点テーブルです。次のクエリでは、これはインライン ビューで表されますaitot内のレコードの総数を示す別のインライン ビューがありますallitems。このカウントは、集計プロジェクターではないため、GROUP BY 句に含める必要があります。

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>

このアプローチが Jeffrey のソリューションよりも優れたパフォーマンスを発揮するかどうかはわかりません。パフォーマンスが低下する可能性は十分にあります (分析クエリは確かに一貫性のある取得が少なくなります)。問題をより明確に強調しているため、単純に興味深いものです。

于 2010-01-28T09:58:56.820 に答える
1

元のクエリのわずかな変更:

Select  
areas,
(
Select 
Count(*)
From 
Allitems Inner_Allitems Left Join Currentitems On (Currentitems.Itemid = Inner_Allitems.Itemid) 
Where inner_allitems.areas = outer_allitems.areas
) *100 / (Select Count(*) From allitems ) as percentage
From allitems outer_allitems
Group By areas
于 2010-01-27T23:22:55.617 に答える
1

最初の簡単なパスは次のとおりです。

select ai.areas,
       (sum(cnt) / max(tot)) * 100 "Percentage Result"
  from (select ai.itemid,
               ai.areas,
               count(ci.itemid) cnt,
               count(ai.areas) over () tot
          from allitems ai
               left outer join
               currentitems ci on (ai.itemid = ci.itemid)
        group by ai.itemid, ai.areas
       ) ai
group by ai.areas

また、テスト データでは、itemid 4 を west に変更する必要があります。

于 2010-01-27T22:12:29.610 に答える