SQl count()帮助select语句

时间:2010-04-26 07:36:05

标签: sql oracle oracle10g ora-00904

我正在使用此代码:

  SELECT MACH_NO, 
         COUNT(MACH_NO) AS TOTAL_REPORTS
    FROM MAINTENANCE_LOG
GROUP BY MACH_NO;

...检索一些数据:

MACH_NO  TOTAL_REPORTS
----------------------
1        4
5        2
8        1
7        1

如何仅检索总报告大于3的位置?我试过了:

WHERE TOTAL_REPORTS > 3 

......但它说

  

ORA-00904:“TOTAL_REPORTS”:标识符无效

4 个答案:

答案 0 :(得分:3)

由于您使用的是GROUP BY,因此您应该使用HAVING而不是WHERE。您还需要明确使用COUNT(MACH_NO)而不是TOTAL_REPORTS的别名。

因此请使用HAVING COUNT(MACH_NO) > 3,而不是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;

答案 1 :(得分:1)

使用HAVING条款

SELECT MACH_NO, COUNT(MACH_NO) AS TOTAL_REPORTS FROM MAINTENANCE_LOG GROUP BY MACH_NO HAVING TOTAL_REPORTS > 3;

答案 2 :(得分:0)

通过MACH_NO从MAINTENANCE_LOG GROUP中选择MACH_NO,COUNT(MACH_NO)AS TOTAL_REPORTS having count(mach_no) > 3

答案 3 :(得分:-1)

而不是WHERE TOTAL_REPORTS> 3,尝试WHERE COUNT(MACH_NO)> 3。