select b.name,b.nature,b.orgName from base_e_route_basesection b 改写SQL如果b.nature等于 还贷性路段 ,则不查询b.name,以b.orgName分组,否则以b.name分组查询
时间: 2023-07-28 07:31:10 浏览: 83
以下是改写后的 SQL 语句:
```
SELECT
CASE WHEN b.nature = '还贷性路段' THEN NULL ELSE b.name END AS name,
b.nature,
b.orgName
FROM
base_e_route_basesection b
GROUP BY
CASE WHEN b.nature = '还贷性路段' THEN b.orgName ELSE b.name END,
b.nature,
b.orgName
```
如果 `b.nature` 等于 '还贷性路段',则将 `b.name` 赋值为 `NULL`,否则保留原值。然后根据不同情况分别进行分组,以实现查询需求。
相关问题
SELECT CASE WHEN b.nature = '还贷性路段' THEN b.orgName ELSE b.NAME END AS NAME, b.nature, b.orgName, SUM(d.authBuildMoney) authBuildMoney, SUM(d.designMoney) designMoney, SUM(d.authSupervisorMoney) authSupervisorMoney FROM base_e_route_basesection b LEFT JOIN system_organization o ON b.org_id = o.organizationId LEFT JOIN mat_plan_planfreeway_detail d on d.sonOrgId = o.organizationId WHERE b.isUse = '1' AND b.orgName IS NOT NULL AND d.parentName = '路面工程' AND d.planType = '养护工程' GROUP BY CASE WHEN b.nature = '还贷性路段' THEN b.orgName ELSE b.NAME END ORDER BY b.nature DESC, o.CODE ASC 这条sql统计有误,如何修改
无法准确判断统计有误的原因,但是可以提供一些可能的修改建议:
1. 检查数据源是否正确,确保统计的数据来源正确;
2. 检查 WHERE 条件是否正确,确保过滤出的数据符合预期;
3. 检查 GROUP BY 是否正确,确保分组的维度和统计的维度一致;
4. 检查 SELECT 和 GROUP BY 是否一致,确保 SELECT 中的字段都在 GROUP BY 中出现过;
5. 检查 ORDER BY 是否正确,确保排序的字段存在且符合预期。
SELECT t1.org_id AS sonOrgId, t1.orgName AS sonOrgName, t1.id AS routeBasesectionId, t1.`name` AS routeBasesectionName, t1.nature, zw.billCode AS diseaseTypeSn, COUNT( t3.id ) AS num, ( CASE WHEN sum( CASE WHEN zw.`status` = '2' THEN zw.workNum END ) IS NOT NULL THEN sum( CASE WHEN zw.`status` = '2' THEN zw.workNum END ) WHEN sum( CASE WHEN zw.`status` = '1' THEN zw.workNum END ) IS NOT NULL THEN sum( CASE WHEN zw.`status` = '1' THEN zw.workNum END ) ELSE sum( CASE WHEN zw.`status` = '0' THEN zw.workNum END ) END ) AS totalAmount FROM mat_zx_section t3 JOIN base_bridge bb ON bb.id = t3.qshMainId LEFT JOIN mat_zx_workload zw ON zw.mainId = t3.id LEFT JOIN base_e_route_basesection t1 ON t3.baseRoute = t1.id LEFT JOIN main_construct t4 ON t3.constructId = t4.id -- 施工申请 LEFT JOIN main_construct_check t5 ON t5.id = t3.acceptanceId -- 施工申请验收 WHERE t5.process_status = '100' GROUP BY zw.billCode, t1.id, t1.nature, t1.org_id ORDER BY t1.org_id, t1.id, zw.billCode UNION ALL SELECT son_org_id AS sonOrgId, son_org_name AS sonOrgName, route_id AS routeBasesectionId, route_name AS routeBasesectionName, route_type AS nature, disease_type_sn AS diseaseTypeSn, num AS num, total_amount AS totalAmount FROM base_history_repair_statistics;这条sql语句执行失败
这个 SQL 语句可能会因为分号(;)导致执行失败。请尝试将分号(;)删除,然后再次执行该 SQL 语句。如下所示:
```
SELECT t1.org_id AS sonOrgId, t1.orgName AS sonOrgName, t1.id AS routeBasesectionId, t1.`name` AS routeBasesectionName, t1.nature, zw.billCode AS diseaseTypeSn, COUNT( t3.id ) AS num, ( CASE WHEN sum( CASE WHEN zw.`status` = '2' THEN zw.workNum END ) IS NOT NULL THEN sum( CASE WHEN zw.`status` = '2' THEN zw.workNum END ) WHEN sum( CASE WHEN zw.`status` = '1' THEN zw.workNum END ) IS NOT NULL THEN sum( CASE WHEN zw.`status` = '1' THEN zw.workNum END ) ELSE sum( CASE WHEN zw.`status` = '0' THEN zw.workNum END ) END ) AS totalAmount FROM mat_zx_section t3 JOIN base_bridge bb ON bb.id = t3.qshMainId LEFT JOIN mat_zx_workload zw ON zw.mainId = t3.id LEFT JOIN base_e_route_basesection t1 ON t3.baseRoute = t1.id LEFT JOIN main_construct t4 ON t3.constructId = t4.id -- 施工申请 LEFT JOIN main_construct_check t5 ON t5.id = t3.acceptanceId -- 施工申请验收 WHERE t5.process_status = '100' GROUP BY zw.billCode, t1.id, t1.nature, t1.org_id ORDER BY t1.org_id, t1.id, zw.billCode UNION ALL SELECT son_org_id AS sonOrgId, son_org_name AS sonOrgName, route_id AS routeBasesectionId, route_name AS routeBasesectionName, route_type AS nature, disease_type_sn AS diseaseTypeSn, num AS num, total_amount AS totalAmount FROM base_history_repair_statistics
```
阅读全文