WITH CTE AS
(SELECT A.DEPARTMENT_ID, A.DEPARTMENT_NAME, B.ITEM_ID, ITEM_PRICE, B.ITEM_STATUS FROM DEPARTMENTS A INNER JOIN PRODUCTION_ITEMS B ON A.DEPARTMENT_ID = B.DEPARTMENT_ID WHERE A.DEPARTMENT_ACTIVE = 'TRUE' AND B.TIME_STAMP BETWEEN @START_DATE AND @END_DATE)
SELECT (SELECT COUNT(DISTINCT ITEM_ID) FROM CTE WHERE ITEM_STATUS = 'R') AS RAGGED
FROM CTE
GROUP BY DEPARTMENT_ID, DEPARTMENT_NAME
ORDER BY DEPARTMENT_NAME ASC
I asked a similar question a couple of weeks ago and I thought I had figured out the answer.. however, this query returns a 0 value for the RAGGED items.
Can anyone tell me where I am going wrong?
a BIG thank you in advance..
Patrick
Patrick Regis