【问题标题】:SQL division gives answer only for the last rowSQL 部门只给出最后一行的答案
【发布时间】:2025-11-29 17:00:02
【问题描述】:

我正在尝试在我的 sql 代码中计算 efficiency,这需要将 2 个变量 vehicle_dispatchedavailable_cars 相除,但它为整列返回相同的答案。这是我的sql代码:

SELECT *,@vehicles_dispatched:=COUNT(DISTINCT v.vehicle_id) AS vehicles_dispatched,
@available_cars:=(SELECT COUNT(vehicle_id) FROM vehicles WHERE company_id=1) AS available_cars,
FORMAT(@vehicles_dispatched / @available_cars,2) AS efficiency
FROM driver_attendance da 
LEFT JOIN vehicles v ON v.vehicle_id=da.vehicle_id
LEFT JOIN collection co ON co.driver_attendance_id=da.driver_attendance_id
LEFT JOIN collectible cb ON cb.collectible_id = co.collectible_id
WHERE company_id=1 GROUP BY attendance_date DESC


示例输出:

vehicles_dispatched available_cars efficiency
        5                 7          0.14
        3                 7          0.14
        6                 7          0.14
        1                 7          0.14


问题是整列都有相同的答案,而不是对每一行进行除法。

这里是更新后的查询,它提供了我想要的输出,这一切都归功于@Ravinder。

SELECT *,FORMAT( vehicle_dispatched /  available_cars, 2 ) AS efficiency
 FROM(
  SELECT da.*, 
    SUM(CASE WHEN attendance_status = 4 THEN 1 ELSE 0 END) AS total_cars_collectible,
    SUM(boundary_payment)+SUM(deficit_payment) AS total_daily_collection,
    ((SUM(boundary_due)+SUM(boundary_deficit)) - (SUM(boundary_payment)+SUM(deficit_payment))) AS total_short,
    @cars_in_maintenance:=(SELECT SUM(DISTINCT(CASE WHEN vehicle_on_duty=2 THEN 1 ELSE 0 END)) AS cars_under_maintenance FROM vehicles WHERE company_id=84) AS cars_in_maintenance,
    @vehicle_dispatched:=COUNT(DISTINCT v.vehicle_id) AS vehicle_dispatched,
    @available_cars:=(SELECT COUNT(vehicle_id) FROM vehicles WHERE company_id=84) AS available_cars
    FROM driver_attendance da 
    LEFT JOIN vehicles    v  ON v.vehicle_id            = da.vehicle_id 
    LEFT JOIN collection  co ON co.driver_attendance_id = da.driver_attendance_id 
    LEFT JOIN collectible cb ON cb.collectible_id       = co.collectible_id 
   WHERE v.company_id = 84
   GROUP BY da.attendance_date DESC
 ) AS vehicles_attended


【问题讨论】:

  • vehicles 表中,vehicle_id 是主键还是外键?因为,如果primary,则不需要distinct vehicle_id
  • 它是一个主键,但是当LEFT JOINdriver_attendance一起使用时有重复的情况,因此我使用了DISTINCT

标签: mysql


【解决方案1】:

countmax 等聚合函数与用户定义的变量一起使用时,结果行为是未知的。我找不到这方面的文档。

不久前我提出了一个类似的问题:
... When a similar statement was executed with aggregate functions like count, sum, group by, the results pattern was entirely different .... Example on SQL Fiddle ...

在您的查询中,您可以先计算各种值的计数,然后在外部查询中计算efficiency

select vehicles_dispatched, available_cars
     , format( vehicles_dispatched /  available_cars, 2 ) as efficiency
 from(
  select da.*, count( distinct v.vehicle_id ) as vehicles_dispatched
       , count( v.vehicle_id ) as available_cars
    from driver_attendance     da 
    left join vehicles    v  on v.vehicle_id            = da.vehicle_id 
    left join collection  co on co.driver_attendance_id = da.driver_attendance_id 
    left join collectible cb on cb.collectible_id       = co.collectible_id 
   where v.company_id = 1
   group by da.attendance_date desc;
 ) as vehicles_attended

【讨论】:

  • 感谢您的回答,我在将它与我的代码集成时做了一些调整,但这确实有很大帮助。干杯!
  • 发布获取正确结果的更新查询。这也会对其他人有所帮助。
  • 好的,我将它添加到我上面的问题中,因为它太长而无法显示为评论。
最近更新 更多