3

我被一个 SQL 查询困住了。我在 select 子句中进行子查询,db2 提示 -119 错误告诉:

"An expression starting with "FACILITY_ID" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified. SQL Code: -119, SQL State: 42803"

我不知道如何在 GROUP BY 中包含 NUMTAGS。我尝试在 GROUP BY 子句中包含 BC.ITEM,但没有结果。我不知道如何在网上找到这个,而且我看过的论坛没有用。

如果有人能解释我如何修复这个问题或粘贴一个我可以寻找解决方案的链接,我将不胜感激。

SQL 查询与 DB2 一起使用,它位于此处:

SELECT SS.NAME, B.DESTINATION_ID, P.DESCRIPTION, B.LAST_READ_DATE, B.LABEL, 
        (SELECT COUNT(BC.ITEM) 
        FROM PROJECT_DB.BOX_CONTENT BC
        WHERE BC.FACILITY_ID = B.FACILITY_ID
        AND BC.BOX_ID = B.BOX_ID
        GROUP BY BC.ITEM) AS NUMTAGS,
        B.BOX_ID
FROM PROJECT_DB.BOX B
INNER JOIN MAINDB.DESTINATION P ON B.DESTINATION_ID = P.DESTINATION_ID
INNER JOIN MAINDB.WAREHOUSE_DESTINATION_SECTION WH 
                                ON B.DESTINATION_ID = WH.DESTINATION_ID
                                AND B.WAREHOUSE_ID = WH.WAREHOUSE_ID
INNER JOIN MAINDB.SECTION S ON S.SECTION = WH.SECTION   
                            AND S.SECTION_TYPE = B.SECTION_TYPE
INNER JOIN MAINDB.DESTINATION_SET SS 
                            ON SS.DESTINATION_SET_ID = S.DESTINATION_SET_ID
WHERE B.WAREHOUSE_ID = 100
GROUP BY B.BOX_ID, B.FACILITY_ID, B.DESTINATION_ID, B.LAST_READ_DATE, B.LABEL, P.DESCRIPTION, SS.NAME
4

3 回答 3

3

您可以通过以下方式更正该语句: v 包括 SELECT 子句中的 GROUP BY 子句中的列,或 v 从 SELECT 子句中删除列。

来自:DB2 文档。

您需要将所有选定的列放在您的 GROUP BY...

于 2013-04-29T17:49:36.000 回答
1

我最后的工作查询是这里。我想我在“group by”或类似的地方引用了错误的表格:

SELECT SS.NAME, P.DESTINATION_ID, P.DESCRIPTION, B.LAST_READ_DATE, B.LABEL, 
        (SELECT COUNT(BC.ITEM) 
        FROM PROJECT_DB.BOX_CONTENT BC
        WHERE BC.ID_FACILITY = B.ID_FACILITY
        AND BC.ID_BOX = B.ID_BOX
        GROUP BY BC.ITEM) AS NUMTAGS, 
        B.BOX_ID, B.FACILITY_ID
FROM PROJECT_DB.BOX B
    INNER JOIN MAINDB.DESTINATION P ON B.DESTINATION_ID = P.DESTINATION_ID
    INNER JOIN MAINDB.WAREHOUSE_DESTINATION_SECTION WH 
                                    ON WH.WAREHOUSE_ID = B.WAREHOUSE_ID 
                                    AND WH.ID_DESTINATION = B.ID_DESTINATION
    INNER JOIN MAINDB.SECTION S ON S.SECTION = WH.SECTION
                                AND S.SECTION_TYPE = B.SECTION_TYPE
    INNER JOIN MAINDB.DESTINATION_SET SS ON SS.DESTINATION_SET_ID = S.DESTINATION_SET_ID
WHERE B.WAREHOUSE_ID = 100
GROUP BY B.BOX_ID, B.FACILITY_ID, P.DESTINATION_ID, B.LAST_READ_DATE, B.LABEL, P.DESCRIPTION, SS.NAME
WITH UR 
于 2012-10-26T14:55:29.123 回答
1

由于您使用的是标量子选择,因此我认为您的GROUP BY任何一个语句都不需要子句。尝试这个:

SELECT 
    SS.NAME,
    B.DESTINATION_ID,
    P.DESCRIPTION,
    B.LAST_READ_DATE,
    B.LABEL,

    (SELECT COUNT(BC.ITEM)
     FROM PROJECT_DB.BOX_CONTENT BC
     WHERE BC.FACILITY_ID = B.FACILITY_ID
     AND BC.BOX_ID = B.BOX_ID
    ) AS NUMTAGS,

    B.BOX_ID
FROM PROJECT_DB.BOX B
JOIN MAINDB.DESTINATION P
  ON B.DESTINATION_ID = P.DESTINATION_ID
JOIN MAINDB.WAREHOUSE_DESTINATION_SECTION WH
  ON B.DESTINATION_ID = WH.DESTINATION_ID
 AND B.WAREHOUSE_ID = WH.WAREHOUSE_ID
JOIN MAINDB.SECTION S 
  ON S.SECTION = WH.SECTION
 AND S.SECTION_TYPE = B.SECTION_TYPE
JOIN MAINDB.DESTINATION_SET SS
  ON SS.DESTINATION_SET_ID = S.DESTINATION_SET_ID
WHERE B.WAREHOUSE_ID = 100
于 2012-10-11T15:33:04.670 回答