2012-10-11 101 views
3

我遇到了SQL查詢。我正在做的SELECT子句中的子查詢和DB2提示一個錯誤-119講:DB2:Select子句中的子集干涉子查詢

"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 

回答

1

我最後的工作查詢是在這裏。我覺得我在引用了錯誤的表格「組中的」或類似的東西:這是在SELECT子句v中包含在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 
2

由於您使用的是標子選擇,我不認爲你需要或者您的語句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 
+0

我所需要的「集團通過」由於WAREHOUSE_DESTINATION_SECTION表的性質。這個表是另一個之間的連接,我沒有限制和過濾結果所需的可變參數,所以我需要「Group by」。 現在事情已經解決了。 – madtyn

3

可以糾正的聲明,或者v從SELECT子句中刪除列 。

來自:DB2文檔。

你需要把ALL選擇列在GROUP BY ...