SELECT f.FAULT_CODE AS "异常代码", f.FAULT_NAME AS "异常名称", COALESCE(r.REPORT_DEPT_CODE, 'N/A') AS "反馈部门", COUNT(r.REPORT_DEPT_CODE) AS "反馈次数", (SELECT COUNT(*) FROM FAULT_REPORT d WHERE d.FAULT_CODE = f.FAULT_CODE) AS "合计反馈次数" FROM FAULT_TYPE f LEFT JOIN FAULT_REPORT r ON f.FAULT_CODE = r.FAULT_CODE WHERE f.BUILT_IN_FLAG = 1 GROUP BY f.FAULT_CODE,f.FAULT_NAME, COALESCE(r.REPORT_DEPT_CODE, 'N/A') ORDER BY f.FAULT_CODE在ORACLE中把此查询SQL用union all在异常代码下加一个009编码并且异常名称下加上合计字段
时间: 2024-03-01 16:50:04 浏览: 65
故障诊断相关的MATLAB代码.rar_fault code_matlab_故障_故障诊断_神经网络故障
5星 · 资源好评率100%
在ORACLE中,将此查询SQL用union all在异常代码下加一个009编码并且异常名称下加上合计字段的方法如下:
```
SELECT f.FAULT_CODE AS "异常代码",
f.FAULT_NAME AS "异常名称",
COALESCE(r.REPORT_DEPT_CODE, 'N/A') AS "反馈部门",
COUNT(r.REPORT_DEPT_CODE) AS "反馈次数",
(SELECT COUNT(*) FROM FAULT_REPORT d WHERE d.FAULT_CODE = f.FAULT_CODE) AS "合计反馈次数"
FROM FAULT_TYPE f
LEFT JOIN FAULT_REPORT r ON f.FAULT_CODE = r.FAULT_CODE
WHERE f.BUILT_IN_FLAG = 1
GROUP BY f.FAULT_CODE,f.FAULT_NAME, COALESCE(r.REPORT_DEPT_CODE, 'N/A')
UNION ALL
SELECT '009' AS "异常代码",
'合计' AS "异常名称",
'N/A' AS "反馈部门",
COUNT(*) AS "反馈次数",
SUM((SELECT COUNT(*) FROM FAULT_REPORT d WHERE d.FAULT_CODE = f.FAULT_CODE)) AS "合计反馈次数"
FROM FAULT_TYPE f
LEFT JOIN FAULT_REPORT r ON f.FAULT_CODE = r.FAULT_CODE
WHERE f.BUILT_IN_FLAG = 1
GROUP BY f.FAULT_CODE,f.FAULT_NAME
ORDER BY "异常代码";
```
在上述SQL语句中,使用UNION ALL操作符将原始查询结果与新增的结果集合并。新增结果集的异常代码为009,异常名称为合计,反馈部门为N/A。同时,使用SUM和子查询的组合,计算出所有异常代码的合计反馈次数,并在查询结果中添加一个合计反馈次数的列。最后,按异常代码进行排序。
阅读全文