0

这是我的 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
4

2 回答 2

0

GROUP BY 子句位于 HAVING 子句之前。这是正确的顺序:

SELECT ...
FROM ...
WHERE ...
GROUP BY ...
HAVING ...
于 2013-08-30T11:51:35.120 回答
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. 此外,min()周围的 list_price 需要位于内部选择的外部。这是一个完整的工作版本:

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
于 2013-08-30T13:11:32.397 回答