这是我的 SQL 查询。我有一个表 A,但是当我尝试使用这个表进入 HAVING (A.CATEGORY) 时显示一个错误ORA-00979: not a GROUP BY expression
。另一方面,如果我将表 A 中的所有代码复制到同一位置,它就会起作用。我怎样才能使这个查询以第一方式正常工作?谢谢
第一个(不正确):
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
第二(正确):
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