SELECT f.PAR_FAULT_CODE AS "parFaultCode", f.FAULT_NAME AS "faultName", COALESCE( r.REPORT_DEPT_CODE, 'N/A' ) AS "reportDeptCode", COUNT( r.REPORT_DEPT_CODE ) AS "report", ( SELECT COUNT( * ) FROM FAULT_REPORT d WHERE d.FAULT_CODE = f.FAULT_CODE ) AS "reportCount" FROM FAULT_TYPE f LEFT JOIN FAULT_REPORT r ON f.FAULT_CODE = r.FAULT_CODE GROUP BY f.PAR_FAULT_CODE, f.FAULT_NAME, COALESCE( r.REPORT_DEPT_CODE, 'N/A' ) ORDER BY f.PAR_FAULT_CODE, f.FAULT_NAME为什么在oracle中报不是group by表达式,请帮我正确修改
时间: 2024-04-04 13:33:24 浏览: 65
故障诊断相关的MATLAB代码.rar_fault code_matlab_故障_故障诊断_神经网络故障
5星 · 资源好评率100%
在Oracle中,如果SELECT语句中的字段包含聚合函数(如COUNT、SUM等),则GROUP BY子句中必须包含所有未聚合的字段。在你的查询中,COUNT(r.REPORT_DEPT_CODE)是一个聚合函数,但是GROUP BY子句中只包含了f.PAR_FAULT_CODE、f.FAULT_NAME和COALESCE(r.REPORT_DEPT_CODE, 'N/A')三个字段,没有包含COUNT(r.REPORT_DEPT_CODE)。因此,Oracle会报错,提示你需要将COUNT(r.REPORT_DEPT_CODE)也包含在GROUP BY子句中。
正确的查询语句如下:
SELECT f.PAR_FAULT_CODE AS "parFaultCode",
f.FAULT_NAME AS "faultName",
COALESCE(r.REPORT_DEPT_CODE, 'N/A') AS "reportDeptCode",
COUNT(r.REPORT_DEPT_CODE) AS "report",
(SELECT COUNT(*) FROM FAULT_REPORT d WHERE d.FAULT_CODE = f.FAULT_CODE) AS "reportCount"
FROM FAULT_TYPE f
LEFT JOIN FAULT_REPORT r ON f.FAULT_CODE = r.FAULT_CODE
GROUP BY f.PAR_FAULT_CODE, f.FAULT_NAME, COALESCE(r.REPORT_DEPT_CODE, 'N/A'), f.FAULT_CODE
ORDER BY f.PAR_FAULT_CODE, f.FAULT_NAME
阅读全文