DB2:通过在Select子句中干扰子查询来分组

时间:2012-10-11 11:48:03

标签: select group-by db2 subquery

我遇到了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

3 个答案:

答案 0 :(得分:3)

  

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

来自:DB2文档。

您需要在GROUP BY ...

中添加所有所选列

答案 1 :(得分: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

答案 2 :(得分: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