2013-07-04 46 views
0

與此Oracle查詢的正確製劑任何幫助理解:Oracle訂單由ORA-00937:不是一個單組基函數

WITH 
    anaq1 as 
     (SELECT 
       POPULATION, 
       YEAR, 
       COUNTRY_OR_AREA 
       FROM POPULATIONS2 
       WHERE 
       country_or_area = 'France' OR 
       country_or_area = 'Brazil' 
     ) 
     select 
      COUNTRY_OR_AREA , 
      COUNT(*) AS OCCURANCES 
      from anaq1 
     ORDER BY COUNT(*) DESC 

回答

2

缺少的GROUP BY子句:

WITH 
    anaq1 as 
    (SELECT 
      POPULATION, 
      YEAR, 
      COUNTRY_OR_AREA 
      FROM POPULATIONS2 
      WHERE 
      country_or_area = 'France' OR 
      country_or_area = 'Brazil' 
    ) 
    select 
     COUNTRY_OR_AREA , 
     COUNT(*) AS OCCURANCES 
     from anaq1 
     GROUP BY COUNTRY_OR_AREA 
     ORDER BY COUNT(*) DESC 

的整個事情可以簡化:

SELECT COUNTRY_OR_AREA, COUNT(*) AS OCCURANCES 
    FROM POPULATIONS2 
    WHERE country_or_area in ('France', 'Brazil') 
    GROUP BY COUNTRY_OR_AREA 
    ORDER BY COUNT(*) DESC 

這是計數一個國家或地區的次數記錄顯示在population2表中。

+0

謝謝!由於內部子查詢每次都不同,因此無法簡化它。我只知道排序列,我需要返回排序的事件 – seedhom