nombre SQl () d'aide avec une instruction select
Question
J'utilise ce code:
SELECT MACH_NO,
COUNT(MACH_NO) AS TOTAL_REPORTS
FROM MAINTENANCE_LOG
GROUP BY MACH_NO;
... pour récupérer des données qui donne:
MACH_NO TOTAL_REPORTS
----------------------
1 4
5 2
8 1
7 1
Comment puis-je récupérer uniquement lorsque des rapports total est plus grand que trois? J'ai essayé:
WHERE TOTAL_REPORTS > 3
... mais il est dit
ORA-00904: "TOTAL_REPORTS": invalide identifiant
La solution
Puisque vous avez utilisé un GROUP BY
, vous devez utiliser HAVING
plutôt que WHERE
. Vous devez également utiliser explicitement COUNT(MACH_NO)
plutôt que votre alias de TOTAL_REPORTS
.
Il faut donc utiliser HAVING COUNT(MACH_NO) > 3
, plutôt que WHERE TOTAL_REPORTS > 3
.
SELECT MACH_NO,
COUNT(MACH_NO) AS TOTAL_REPORTS
FROM MAINTENANCE_LOG
GROUP BY MACH_NO
HAVING COUNT(MACH_NO) > 3;
Autres conseils
Utilisez AYANT CLAUSE
SELECT MACH_NO, COUNT(MACH_NO) AS TOTAL_REPORTS FROM MAINTENANCE_LOG GROUP BY MACH_NO HAVING TOTAL_REPORTS > 3;
SELECT MACH_NO, COUNT (MACH_NO) AS TOTAL_REPORTS DE MAINTENANCE_LOG GROUP BY MACH_NO
having count(mach_no) > 3
Au lieu de WHERE TOTAL_REPORTS> 3, essayez OU COUNT (MACH_NO)> 3.