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", SUM(COUNT(r.REPORT_DEPT_CODE)) OVER (PARTITION BY 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在fauleName加一个标题为合计字段 并且根据reportDeptCode分组算出对应的reportCount
时间: 2024-03-21 13:42:00 浏览: 69
failslab.rar_V2
可以在SELECT语句中使用CASE WHEN语句来对 faultName 进行处理,添加 "合计" 字段。具体的SQL语句如下所示:
```
SELECT f.PAR_FAULT_CODE AS "parFaultCode",
CASE WHEN COALESCE(r.REPORT_DEPT_CODE, 'N/A') = 'N/A' THEN f.FAULT_NAME
ELSE f.FAULT_NAME || ' 合计' END AS "faultName",
COALESCE(r.REPORT_DEPT_CODE, 'N/A') AS "reportDeptCode",
COUNT(r.REPORT_DEPT_CODE) AS "report",
SUM(COUNT(r.REPORT_DEPT_CODE)) OVER (PARTITION BY 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;
```
在这个SQL语句中,我们使用了 CASE WHEN 语句来判断 reportDeptCode 是否为 'N/A',如果是则不添加 "合计" 字段,否则在 faultName 后面添加 " 合计"。最后按照指定的字段进行分组和排序,计算 reportCount。
阅读全文