title: DB2: group by interfering subquery in Select clause
body: <p>I'm stuck with a SQL query. I'm making a subquery in the select clause and db2 prompts a -119 error telling:</p>
  
  <pre><code>"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"
  </code></pre>
  
  <p>I don't know how to include NUMTAGS in the GROUP BY. I tried including the BC.ITEM in the GROUP BY clause with no result. I don't know how exactly to look for this in the web, and the forums I looked at were no useful.</p>
  
  <p>If someone could explain me how to mend this or paste a link where I could look for a solution, I would be grateful.</p>
  
  <p>The SQL query is with DB2 and it's down here:</p>
  
  <pre><code>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
  </code></pre>