SQL - 每天的平均距离

时间:2013-10-01 14:52:20

标签: sql

我在这里遇到一个问题:“显示每天由巴黎汽车驾驶的平均距离”

我还有2个表引用了这个问题

  1. table_carsidbrandtypelicense
  2. table_distancesid_cardatedistance
  3. 我设法选择“巴黎汽车的平均距离”

     select avg(table_distances.distance)
         from table_distances
         INNER JOIN table_cars ON table_distances.id_car = table_cars.id
         where table_cars.license = 'Paris';'
    

    尽管如此,我每天的平均距离仍有问题。我查看了stackoverflow / google上的相关问题,但我更加困惑。

    有人可以解释我如何改进查询以显示每天的平均距离吗?

3 个答案:

答案 0 :(得分:0)

只需将日期添加到您选择的内容并按日期分组,以便按行平均:

SELECT table_distances.date, avg(table_distances.distance)
FROM table_distances
  INNER JOIN table_cars ON table_distances.id_car = table_cars.id
WHERE table_cars.license = 'Paris'
GROUP BY table_distances.date

答案 1 :(得分:0)

这可以让你了解每个车每个日期的距离。

SELECT id_car, date, AVG(table_distances.distance) 
FROM table_distances
INNER JOIN table_cars 
ON table_distances.id_car = table_cars.id
WHERE table_cars.license = 'Paris'
GROUP BY id_car, date
ORDER BY id_car, date

答案 2 :(得分:0)

只需将GROUP BY子句添加到DATE列

即可 SQL GROUP BY子句

Reference

SELECT AVG(td.distance)
     FROM table_distances td
     INNER JOIN table_cars tc ON td.id_car = tc.id
     WHERE tc.license = 'Paris'
     GROUP BY td.date;

然后你必须得到每天一辆车的平均距离