SELECT 0,sum(number) from ( SELECT inc.disposal_result as results, COUNT() as number FROM soc_data.incidents inc JOIN soc_data.incidents_monitor mon ON inc.incidents_id = mon.incidents_id WHERE inc.disposal_result IS NOT NULL and inc.disposal_result ='3' GROUP BY results) a UNION ALL SELECT 1,sum(number) from ( SELECT inc.disposal_result as results, COUNT() as number FROM soc_data.incidents inc JOIN soc_data.incidents_monitor mon ON inc.incidents_id = mon.incidents_id WHERE inc.disposal_result IS NOT NULL and inc.disposal_result !='3' GROUP BY results) b 优化sql保证输出的结果为 第一行 0,结果,第二行 1,结果
时间: 2024-02-26 19:58:44 浏览: 84
data_disposal.rar_cd4094
可以尝试使用如下的 SQL 语句进行优化:
```
SELECT 0, SUM(CASE WHEN inc.disposal_result = '3' THEN 1 ELSE 0 END) AS result_0
FROM soc_data.incidents inc
JOIN soc_data.incidents_monitor mon ON inc.incidents_id = mon.incidents_id
WHERE inc.disposal_result IS NOT NULL;
UNION ALL
SELECT 1, SUM(CASE WHEN inc.disposal_result != '3' THEN 1 ELSE 0 END) AS result_1
FROM soc_data.incidents inc
JOIN soc_data.incidents_monitor mon ON inc.incidents_id = mon.incidents_id
WHERE inc.disposal_result IS NOT NULL;
```
这里使用了 `CASE WHEN` 语句来计算符合条件的数量,避免了使用子查询的开销,从而提高了查询效率。同时,将 `GROUP BY` 语句去掉,也可以减少查询时间。
阅读全文