這是我的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
如果我刪除GROUP BY,讓我看到同樣的錯誤「ORA-00979:不是一個GROUP BY表達式」 我想使用A.CATEGORY ..但A.CATEGORY不能在裏面工作..我可以如何使用「一張桌子變成了? – DesignPuma
還有一個問題 - 'HAVING'子句中的'MIN()'需要超出SELECT。我會更新我的答案。 – Joe
錯誤再次發生: ORA-01427:單行子查詢返回多個行 – DesignPuma