2013-08-30 97 views
0

這是我的SQL查詢。我有一個表A,但是當我嘗試使用此表時,HAVING(A.CATEGORY)顯示一個錯誤ORA-00979: not a GROUP BY expression。另一種方式,如果我將表A中的所有代碼複製到同一個地方,它就可以工作。我如何使這個查詢在第一路上正常工作?由於如何讓這個sql查詢以第一種方式工作?

FIRST(不正確):

SELECT 
    DEMO_PRODUCT_INFO.PRODUCT_NAME 
FROM 
    DEMO_PRODUCT_INFO, 
    (SELECT 
     DEMO_PRODUCT_INFO.CATEGORY 
    FROM 
     DEMO_PRODUCT_INFO 
    HAVING 
     MAX(DEMO_PRODUCT_INFO.LIST_PRICE) 
     = (SELECT MAX(MAX(DEMO_PRODUCT_INFO.LIST_PRICE)) 
      FROM DEMO_PRODUCT_INFO 
      GROUP BY DEMO_PRODUCT_INFO.CATEGORY) 
    GROUP BY 
     DEMO_PRODUCT_INFO.CATEGORY) A 
WHERE 
    DEMO_PRODUCT_INFO.CATEGORY = A.CATEGORY 
HAVING 
    MIN(DEMO_PRODUCT_INFO.LIST_PRICE) 
    = (SELECT MIN(DEMO_PRODUCT_INFO.LIST_PRICE) 
     FROM DEMO_PRODUCT_INFO 
     WHERE DEMO_PRODUCT_INFO.CATEGORY = A.CATEGORY 
     GROUP BY DEMO_PRODUCT_INFO.CATEGORY) 
GROUP BY 
    DEMO_PRODUCT_INFO.PRODUCT_NAME 

SECOND(正確):

SELECT 
    DEMO_PRODUCT_INFO.PRODUCT_NAME 
FROM 
    DEMO_PRODUCT_INFO, 
    (SELECT 
     DEMO_PRODUCT_INFO.CATEGORY 
    FROM 
     DEMO_PRODUCT_INFO 
    HAVING 
     MAX(DEMO_PRODUCT_INFO.LIST_PRICE) 
     = (SELECT MAX(MAX(DEMO_PRODUCT_INFO.LIST_PRICE)) 
      FROM DEMO_PRODUCT_INFO 
      GROUP BY DEMO_PRODUCT_INFO.CATEGORY) 
    GROUP BY 
     DEMO_PRODUCT_INFO.CATEGORY) A 
WHERE 
    DEMO_PRODUCT_INFO.CATEGORY = A.CATEGORY 
HAVING 
    MIN(DEMO_PRODUCT_INFO.LIST_PRICE) 
    = (SELECT MIN(DEMO_PRODUCT_INFO.LIST_PRICE) 
     FROM DEMO_PRODUCT_INFO 
     WHERE DEMO_PRODUCT_INFO.CATEGORY 
      = (SELECT DEMO_PRODUCT_INFO.CATEGORY 
        FROM DEMO_PRODUCT_INFO 
       HAVING MAX(DEMO_PRODUCT_INFO.LIST_PRICE) 
         = (SELECT MAX(MAX(DEMO_PRODUCT_INFO.LIST_PRICE)) 
          FROM DEMO_PRODUCT_INFO 
          GROUP BY DEMO_PRODUCT_INFO.CATEGORY) 
       GROUP BY DEMO_PRODUCT_INFO.CATEGORY) 
     GROUP BY DEMO_PRODUCT_INFO.CATEGORY) 
GROUP BY 
    DEMO_PRODUCT_INFO.PRODUCT_NAME 

回答

0

我不是你想要做的100%,但這個錯誤是因爲你的腳本的這一部分:

HAVING 
    MIN(DEMO_PRODUCT_INFO.LIST_PRICE) 
    = (SELECT MIN(DEMO_PRODUCT_INFO.LIST_PRICE) 
     FROM DEMO_PRODUCT_INFO 
     WHERE DEMO_PRODUCT_INFO.CATEGORY = A.CATEGORY 
     GROUP BY DEMO_PRODUCT_INFO.CATEGORY) 

select min(ilst_price)...group by category會產生list_prices的列表。因此,having子句有效地指出「只有最小list_prices 等於最小list_prices列表的行,按類別分組」。這當然是不合邏輯的。

我想你想要最小的list_price類別?您將需要刪除group by。此外,list_price周圍的min()需要位於內部選擇的外部。下面是一個完整的工作版本:

SELECT 
    DEMO_PRODUCT_INFO.PRODUCT_NAME 
FROM 
    DEMO_PRODUCT_INFO, 
    (SELECT 
     DEMO_PRODUCT_INFO.CATEGORY 
    FROM 
     DEMO_PRODUCT_INFO 
    HAVING 
     MAX(DEMO_PRODUCT_INFO.LIST_PRICE) 
     = (SELECT MAX(MAX(DEMO_PRODUCT_INFO.LIST_PRICE)) 
      FROM DEMO_PRODUCT_INFO 
      GROUP BY DEMO_PRODUCT_INFO.CATEGORY) 
    GROUP BY 
     DEMO_PRODUCT_INFO.CATEGORY) A 
WHERE 
    DEMO_PRODUCT_INFO.CATEGORY = A.CATEGORY 
HAVING 
    MIN(DEMO_PRODUCT_INFO.LIST_PRICE) 
    = MIN((SELECT MIN(DEMO_PRODUCT_INFO.LIST_PRICE) 
     FROM DEMO_PRODUCT_INFO 
     WHERE DEMO_PRODUCT_INFO.CATEGORY = A.CATEGORY)) 
GROUP BY 
    DEMO_PRODUCT_INFO.PRODUCT_NAME 
+0

如果我刪除GROUP BY,讓我看到同樣的錯誤「ORA-00979:不是一個GROUP BY表達式」 我想使用A.CATEGORY ..但A.CATEGORY不能在裏面工作..我可以如何使用「一張桌子變成了? – DesignPuma

+0

還有一個問題 - 'HAVING'子句中的'MIN()'需要超出SELECT。我會更新我的答案。 – Joe

+0

錯誤再次發生: ORA-01427:單行子查詢返回多個行 – DesignPuma

0

GROUP BY子句去HAVING子句之前。這是正確的順序:

SELECT ... 
FROM ... 
WHERE ... 
GROUP BY ... 
HAVING ... 
+0

這是同樣的.. – DesignPuma

+0

甲骨文將接受''前GROUP BY' HAVING':'選擇和從雙有總和(1)(1)> 1組由dummy'會工作。 – Joe