I’m using this code:
SELECT MACH_NO,
COUNT(MACH_NO) AS TOTAL_REPORTS
FROM MAINTENANCE_LOG
GROUP BY MACH_NO;
…to retrieve some data which gives:
MACH_NO TOTAL_REPORTS
----------------------
1 4
5 2
8 1
7 1
How can I retrieve only where total reports is bigger than three? I tried:
WHERE TOTAL_REPORTS > 3
…but it says
ORA-00904: “TOTAL_REPORTS”: invalid identifier
Since you’ve used a
GROUP BY, you should useHAVINGrather thanWHERE. You also need to explicitly useCOUNT(MACH_NO)rather than your alias ofTOTAL_REPORTS.So use
HAVING COUNT(MACH_NO) > 3, rather thanWHERE TOTAL_REPORTS > 3.