0

我有一个问题:“显示每天从巴黎开车的平均距离”

我也有 2 个表提到了这个问题

  1. table_cars: id, brand, type,license
  2. table_distances: id_car, date,distance

我设法选择了“从巴黎出发的汽车的平均距离”

 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 上的相关问题,但我更加困惑。

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

4

3 回答 3

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
于 2013-10-01T14:54:43.690 回答
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
于 2013-10-01T14:57:01.830 回答
0

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

SQL GROUP BY 子句的参考

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;

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

于 2013-10-01T14:57:27.783 回答