WITH T AS ( SELECT 100 AS ID, 1 AS STATUS FROM DUAL UNION ALL SELECT 200 AS ID, 1 AS STATUS FROM DUAL UNION ALL SELECT 100 AS ID, 2 AS STATUS FROM DUAL UNION ALL SELECT 100 AS ID, 2 AS STATUS FROM DUAL UNION ALL SELECT 200 AS ID, 2 AS STATUS FROM DUAL UNION ALL SELECT 300 AS ID, 2 AS STATUS FROM DUAL ) SELECT ID, COUNT(ID) CNT FROM T WHERE 1=1 AND STATUS = 2 GROUP BY ID ORDER BY ID;