优化Mysql语句:SET __startTime_Date_1 = TIMESTAMP 2023-09-20 000000;SET __endTime_Date_2 = TIMESTAMP 2023-09-20 000000;SELECT COUNT AS OrderCount COALESCESUMt2CompleteOrderCount 0 AS CompleteOrderCount COA
优化后的Mysql语句如下所示:
SELECT COUNT(*) AS OrderCount, COALESCE(SUM(t2.CompleteOrderCount), 0) AS CompleteOrderCount, COALESCE(SUM(t2.LeaveCarCount), 0) AS LeaveCarCount, COALESCE(SUM(t2.BeforeDiffMinute), 0) AS BeforeDiffMinute, COALESCE(SUM(t2.CompleteDiffMinute), 0) AS CompleteDiffMinute, COALESCE(SUM(t2.LaterDiffMinute), 0) AS LaterDiffMinute FROM ( SELECT t1.CompleteOrderCount, t1.LeaveCarCount, t1.BeforeDiffMinute, t1.CompleteDiffMinute, t1.LaterDiffMinute, 1 AS Key FROM ( SELECT COUNT(CASE WHEN c.OrderStatus = '4' THEN 1 END) AS CompleteOrderCount, COUNT(CASE WHEN t0.LeaveTime IS NOT NULL THEN 1 END) AS LeaveCarCount, COALESCE(SUM(TIMESTAMPDIFF(MINUTE, t0.EnterTime, c.ChargingBeginTime)), 0) AS BeforeDiffMinute, COALESCE(SUM(COALESCE(TIMESTAMPDIFF(MINUTE, c.ChargingBeginTime, c.ChargingEndTime), 0)), 0) AS CompleteDiffMinute, COALESCE(SUM(COALESCE(TIMESTAMPDIFF(MINUTE, c.ChargingEndTime, t0.LeaveTime), 0)), 0) AS LaterDiffMinute FROM Charging_Order AS c LEFT JOIN ( SELECT e.Id, e.CarNameOrDirectory, e.ChargeElectricity, e.ChargingPileId, e.CreateTime, e.CreatorId, e.CurrentA, e.CurrentB, e.CurrentC, e.Deleted, e.DischargeElectricity, e.EquipmentModel, e.EquipmentName, e.GunsStatus, e.HideList, e.InterfaceType, e.IsEnable, e.LockStatus, e.LowerRatedVoltage, e.ManufacturerId, e.ManufacturerName, e.Modifier, e.ModifyTime, e.NameOrDirectory, e.NationalStandard, e.POI, e.ParkStatus, e.ParkingLot, e.ProductionDate, e.RatedCurrent, e.RatedPower, e.SOC, e.UpperRatedVoltage, e.VoltageA, e.VoltageB, e.VoltageC, e0.Id AS Id0, e0.CarNameOrDirectory AS CarNameOrDirectory0, e0.ChargingArea, e0.ChargingGunsCount, e0.CreateTime AS CreateTime0, e0.CreatorId AS CreatorId0, e0.Deleted AS Deleted0, e0.EnterpriseId, e0.EquipmentLat, e0.EquipmentLng, e0.EquipmentModel AS EquipmentModel0, e0.EquipmentName AS EquipmentName0, e0.EquipmentType, e0.EquipmentUsageAreaTypeId, e0.GrossPower, e0.HideList AS HideList0, e0.IsEnable AS IsEnable0, e0.ManufacturerId AS ManufacturerId0, e0.ManufacturerName AS ManufacturerName0, e0.Modifier AS Modifier0, e0.ModifyTime AS ModifyTime0, e0.NameOrDirectory AS NameOrDirectory0, e0.POI AS POI0, e0.ProductionDate AS ProductionDate0, e0.SortNum FROM Equipment_ChargingGunsInfo AS e LEFT JOIN Equipment_ChargingPileInfo AS e0 ON e.ChargingPileId = e0.Id WHERE e.Deleted = 0 AND e.IsEnable = 1 AND e0.Deleted = 0 AND e0.IsEnable = 1 AND e0.EnterpriseId = '575dbeb8-4af1-11ee-8854-0c9d920f0143' ) AS t ON c.ChargingGunId = t.Id LEFT JOIN ( SELECT s.CarLicenseNumber, s.EnterTime, s.LeaveTime FROM Security_CarEnterLeaveInfo AS s WHERE s.Deleted = 0 AND s.IsEnable = 1 ) AS t0 ON c.PlateNum = t0.CarLicenseNumber WHERE c.Deleted = 0 AND ( (CONVERT(c.ChargingBeginTime, DATE) >= @__startTime_Date_1 AND CONVERT(COALESCE(c.ChargingEndTime, CURRENT_TIMESTAMP()), DATE) <= @__endTime_Date_2) OR ( (CONVERT(c.ChargingBeginTime, DATE) <= @__startTime_Date_1 AND CONVERT(COALESCE(c.ChargingEndTime, CURRENT_TIMESTAMP()), DATE) >= @__endTime_Date_2) AND CONVERT(COALESCE(c.ChargingEndTime, CURRENT_TIMESTAMP()), DATE) <= @__endTime_Date_2 ) OR ( (CONVERT(c.ChargingBeginTime, DATE) >= @__startTime_Date_1 AND CONVERT(c.ChargingBeginTime, DATE) <= @__endTime_Date_2) OR (CONVERT(c.ChargingBeginTime, DATE) <= @__startTime_Date_1 AND CONVERT(COALESCE(c.ChargingEndTime, CURRENT_TIMESTAMP()), DATE) >= @__endTime_Date_2) ) ) GROUP BY c.Id, c.PlateNum, c.OrderStatus, c.ChargingBeginTime, c.ChargingEndTime, t0.EnterTime, t0.LeaveTime, TIMESTAMPDIFF(MINUTE, t0.EnterTime, c.ChargingBeginTime), TIMESTAMPDIFF(MINUTE, c.ChargingBeginTime, c.ChargingEndTime), TIMESTAMPDIFF(MINUTE, c.ChargingEndTime, t0.LeaveTime) ) AS t1 ) AS t2 GROUP BY t2.Key
原文地址: https://www.cveoy.top/t/topic/jd5M 著作权归作者所有。请勿转载和采集!