检查下以下语句哪里有错误 select CONCAT(((sum(name is null)) / sum(name)) * 100,%) as '空值占比' from pub_rtlcustomer
时间: 2024-02-17 10:05:08 浏览: 23
以下是正确的SQL语句:
```
SELECT CONCAT(((SUM(name IS NULL)) / SUM(name)) * 100, '%') AS '空值占比' FROM pub_rtlcustomer;
```
在原始语句中,百分号未被转义,应该加上单引号。正确的写法是 `'%')`。
相关问题
MySQL优化:select brandName,have_vin_count,(select count(1) from epc_carmodel_skucount where ifnull(vin,'') <> '') as have_vin_sum_count, concat(round(have_vin_count/(select count(1) from epc_carmodel_skucount where ifnull(vin,'') <> '')*100, 2),'%') as have_vin_percent from (select b.brandName,count(distinct a.vehicleUUID) as have_vin_count from (select distinct vehicleUUID from epc_carmodel_skucount where ifnull(vin,'') <> '') a inner join ( select b.brandName,b.vehicleUUID from epc_material_sence_model a join ( select * from epc_bom_sence where brandName not in ('演示','测试','客服内测') and ifnull(brandName,'') <> '' and brandName not like '火星%' and brandName not like '原厂%' ) b on a.oeId=b.oeId where a.categoryName='火花塞' ) b on a.vehicleUUID=b.vehicleUUID group by b.brandName
这段 SQL 语句是查询汽车品牌的火花塞车型中,有 VIN 码的车型数量及占比。其中使用了子查询和连接查询,可能会有性能问题。建议优化如下:
1. 尽量避免在子查询中使用聚合函数,可以将子查询的结果存储到临时表中,以提高查询效率。
2. 将子查询中的 ifnull(vin,'') <> '' 转换为 vin IS NOT NULL AND vin <> '',可以利用索引优化查询。
3. 使用 JOIN 操作时,尽量通过 ON 子句指定连接条件,避免使用 WHERE 子句进行过滤,提高查询效率。
优化后的 SQL 语句如下:
```
SELECT
b.brandName,
COUNT(DISTINCT a.vehicleUUID) AS have_vin_count,
COUNT(DISTINCT c.vehicleUUID) AS have_vin_sum_count,
CONCAT(ROUND(COUNT(DISTINCT a.vehicleUUID) / COUNT(DISTINCT c.vehicleUUID) * 100, 2), '%') AS have_vin_percent
FROM
epc_carmodel_skucount a
INNER JOIN (
SELECT
b.brandName,
b.vehicleUUID
FROM
epc_material_sence_model a
JOIN (
SELECT *
FROM epc_bom_sence
WHERE brandName NOT IN ('演示', '测试', '客服内测')
AND IFNULL(brandName, '') <> ''
AND brandName NOT LIKE '火星%'
AND brandName NOT LIKE '原厂%'
) b
ON a.oeId = b.oeId
WHERE a.categoryName = '火花塞'
) b
ON a.vehicleUUID = b.vehicleUUID
AND a.vin IS NOT NULL
AND a.vin <> ''
LEFT JOIN epc_carmodel_skucount c
ON c.vin IS NOT NULL
AND c.vin <> ''
GROUP BY
b.brandName
```
这样,可以避免子查询中的聚合函数,使用 INNER JOIN 操作指定连接条件,同时使用 LEFT JOIN 操作获取所有有 VIN 码的车型数量,从而提高查询效率。
SQL优化以下语句(select f.file_name,a.content_id,c.fd_objectid level_id,c.level level_val,e.fd_objectid manage_id, ifnull((select count(fd_objectid) from message_receiver where MESSAGE_ID = e.fd_objectid), 0) SEND_PEOPLE_NUM, ifnull((select sum(case when reply_content is not null and reply_content != '' then 1 else 0 end) from message_receiver where MESSAGE_ID = e.fd_objectid), 0) reply_num, ifnull((select count(fd_objectid) from (select * from (select *,row_number() over(partition by seq,sendee_tel order by call_stat desc) flag from GROUPCALL_DETAILS) where flag = '1') where busi_id like concat('%', a.content_id) and busi_id like concat(a.event_id, '%')), 0) call_all, ifnull((select sum(case when call_stat like '%0%' then 1 else 0 end) from (select * from (select *,row_number() over(partition by seq,sendee_tel order by call_stat desc) flag from GROUPCALL_DETAILS) where flag = '1') where busi_id like concat('%', a.content_id) and busi_id like concat(a.event_id, '%')), 0) call_jt from NWYJ_SERVICE.ECM_EMYA_ORDER a left join MAP_EMEC_PLAN_CONTENT b on b.FD_OBJECTID = a.CONTENT_ID left join MAP_EMEC_PLAN c on c.FD_OBJECTID = b.RELATION_ID left join MAP_EMEC_ORG_RELATION d on d.FD_OBJECTID = b.ORG_RELATION_ID left join MESSAGE_MANAGE e on e.BUSI_ID = a.FD_OBJECTID left join MAP_EMEC_PLAN_ORG_TREE f on f.fd_objectid = d.org_id where a.event_id = #{eventId} and a.is_del = '0' and b.is_del = '0' and c.is_del = '0' and d.is_del = '0' and f.is_del = '0' and c.fd_objectid = #{levelId} and e.fd_objectid is not null)
以下是对给定的SQL语句进行优化的建议:
1. 使用连接查询代替子查询:将子查询转换为连接查询可以提高查询性能。可以使用LEFT JOIN来连接子查询的结果。
2. 使用COUNT()和SUM()函数的条件表达式:在使用COUNT()和SUM()函数时,可以使用条件表达式替代子查询,从而减少子查询的数量。
3. 使用预编译语句:使用预编译语句可以提高SQL的执行效率,并且可以防止SQL注入攻击。
4. 添加索引:根据查询中的条件列,可以考虑添加索引以提高查询性能。
下面是优化后的SQL语句:
```sql
SELECT
f.file_name, a.content_id, c.fd_objectid AS level_id, c.level AS level_val, e.fd_objectid AS manage_id,
IFNULL((
SELECT COUNT(fd_objectid)
FROM message_receiver
WHERE MESSAGE_ID = e.fd_objectid
), 0) AS SEND_PEOPLE_NUM,
IFNULL((
SELECT SUM(CASE WHEN reply_content IS NOT NULL AND reply_content != '' THEN 1 ELSE 0 END)
FROM message_receiver
WHERE MESSAGE_ID = e.fd_objectid
), 0) AS reply_num,
IFNULL((
SELECT COUNT(fd_objectid)
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY seq, sendee_tel ORDER BY call_stat DESC) flag
FROM GROUPCALL_DETAILS
) AS temp
WHERE flag = '1' AND busi_id LIKE CONCAT('%', a.content_id) AND busi_id LIKE CONCAT(a.event_id, '%')
), 0) AS call_all,
IFNULL((
SELECT SUM(CASE WHEN call_stat LIKE '%0%' THEN 1 ELSE 0 END)
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY seq, sendee_tel ORDER BY call_stat DESC) flag
FROM GROUPCALL_DETAILS
) AS temp
WHERE flag = '1' AND busi_id LIKE CONCAT('%', a.content_id) AND busi_id LIKE CONCAT(a.event_id, '%')
), 0) AS call_jt
FROM NWYJ_SERVICE.ECM_EMYA_ORDER a
LEFT JOIN MAP_EMEC_PLAN_CONTENT b ON b.FD_OBJECTID = a.CONTENT_ID
LEFT JOIN MAP_EMEC_PLAN c ON c.FD_OBJECTID = b.RELATION_ID
LEFT JOIN MAP_EMEC_ORG_RELATION d ON d.FD_OBJECTID = b.ORG_RELATION_ID
LEFT JOIN MESSAGE_MANAGE e ON e.BUSI_ID = a.FD_OBJECTID
LEFT JOIN MAP_EMEC_PLAN_ORG_TREE f ON f.fd_objectid = d.org_id
WHERE a.event_id = #{eventId}
AND a.is_del = '0'
AND b.is_del = '0'
AND c.is_del = '0'
AND d.is_del = '0'
AND f.is_del = '0'
AND c.fd_objectid = #{levelId}
AND e.fd_objectid IS NOT NULL
```
请注意,这些优化建议仅供参考,实际效果可能因数据库结构和数据量而有所不同。建议在进行任何更改之前,在测试环境中进行充分的测试和评估。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)