2010-01-27 8 views
5

मैं किसी निश्चित क्षेत्र में उपलब्ध आइटमिड का प्रतिशत प्राप्त करने का प्रयास कर रहा हूं। मेरी क्वेरी का उपयोग करना, मैं एक त्रुटि ORA-00937: not a single-group group functionओआरए -00 9 37 को कैसे हल करें: प्रतिशत की गणना करते समय एकल समूह समूह फ़ंक्शन नहीं?

सभी जानकारी प्राप्त:

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 

जब मैं एसक्यूएल सर्वर में ठीक उसी क्वेरी चलाने, यह ठीक काम करता है। ओरेकल में इसे कैसे ठीक किया जाए?

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 जरूरतों में पश्चिम में परिवर्तित करने की:

+0

महान प्रतिक्रियाओं के लिए धन्यवाद। ऐसा करने के विभिन्न तरीके! धन्यवाद। मैंने यह भी पाया कि एक समूह जोड़कर (प्रश्न संपादित किया गया है, "***" के लिए मेरी क्वेरी में देखें) यह भी काम कर रहा है। – Liao

उत्तर

7

एनालिटिक्स अपने दोस्त हैं:

SELECT DISTINCT 
     areas 
     ,COUNT(currentitems.itemid) 
     OVER (PARTITION BY areas) * 100 
    /COUNT(*) OVER() Percentage 
FROM allitems, currentitems 
WHERE allitems.itemid = currentitems.itemid(+); 
+1

वह SQL सर्वर और ओरेकल का उल्लेख करता है, शायद क्वेरी पोर्टेबल होना चाहिए, जो विक्रेता अज्ञेयवादी है। –

+3

पोर्टेबिलिटी shmortability :) –

1

यहां एक त्वरित पहले पास होना जरूरी है।

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 
3
बस इसके बारे में ओह, एनालिटिक्स के बिना इसे करने का एक तरीका के लिए

जेफरी के समाधान को areas के डुप्लिकेशन के कारण एक DISTINCT की आवश्यकता थी। allitems तालिका वास्तव में currentitems और एक पुष्टिक areas तालिका के बीच एक चौराहे तालिका है। निम्नलिखित क्वेरी में यह इनलाइन व्यू ai द्वारा दर्शाया गया है। एक और इनलाइन व्यू tot है जो हमें allitems में रिकॉर्ड्स की कुल संख्या संख्या देता है। इस गणना को ग्रुप बाय क्लॉज में शामिल किया जाना चाहिए, क्योंकि यह एक समेकित प्रोजेक्टर नहीं है।

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> 

मैं नहीं जानता कि क्या इस दृष्टिकोण जेफरी के समाधान की तुलना में बेहतर प्रदर्शन करेंगे: यह काफी संभवतः बदतर प्रदर्शन करेंगे (एनालिटिक्स क्वेरी निश्चित रूप से हो जाता है कम संगत है)। यह दिलचस्प है क्योंकि यह मुद्दों को और अधिक स्पष्ट रूप से हाइलाइट करता है।

+1

फिर भी, आपके इनपुट के लिए धन्यवाद। मुझे आज समय नहीं मिला, लेकिन मैं कल सभी महान उत्तरों का परीक्षण करूँगा! – Liao

संबंधित मुद्दे