I have a problem here: "Show average distance per day driven by cars from Paris"
I have also 2 tables referred to this problem
table_cars
: id
, brand
, type
, license
table_distances
: id_car
, date
, distance
I have managed to select "the average distance for the cars from Paris"
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';'
Though, I have still a problem with average distance per day. I looked over related questions on the stackoverflow/google but I got more confused.
Can somebody explain how I can improve my query to show average distance per day?
Simply add the date to what you select and group by it so it's averaged per row:
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
This should get you the distances per car per date.
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
Simply add GROUP BY
clause to your DATE column
Reference for SQL GROUP BY clause
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;
Then you have to get average distance of a car per day
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.