从下面的表格中,我需要确定哪个城市受到公交车的污染最多。
路由:
route_id | departure_city | destination_city | bus_type | times_per_day
1 2 1 1 4
2 1 3 2 2
3 3 1 2 1
4 1 2 1 5
5 1 3 1 3bustypes:
bus_type_id | pollution_output
1 3
2 7例如,城市2每天暴露于bus_type 1 4次(route_id 1)和bus_type 1每天5次(route_id 4),每天产生27次污染。但我基本上需要计算所有城市的这个值,并返回污染最大的一个,我该怎么做呢?
发布于 2013-03-19 05:53:25
SELECT city, sum(pollution) AS total_pollution
FROM (
SELECT r.depature_city AS city
,b.pollution_output * r.times_per_day AS pollution
FROM routes r
JOIN bustypes b ON b.bus_type_id = r.bus_type
UNION ALL
SELECT r.destination_city
,b.pollution_output * r.times_per_day
FROM routes r
JOIN bustypes b ON b.bus_type_id = r.bus_type
) AS sub
GROUP BY cityhttps://stackoverflow.com/questions/15487397
复制相似问题