上下文
我正在编写一个应用程序来改变车辆路径问题。该应用程序有路线,停靠点和路线的行车路线。我需要为视图编写一个查询,该视图结合了路径的所有相关属性。因此,我需要在一个查询中将routes表连接到多个多对多的关系。
查询详情
有路由表,route_stop_join表和路由方向表。路由和停靠之间的关系确实很多,但我们只需要一个停止ID列表,因此可以考虑与连接表的一对多关系。以下查询计算n次的总和,其中n是停靠次数:
select r.id,
array_agg(j.stop_id) as stops,
sum(rd.time_elapsed) as total_time,
sum(rd.drive_distance) as total_distance
from routes_directions rd
right join routes r
on rd.route_id = r.id
left join routes_stops_join j
on r.id = j.route_id
group by r.id;
我可以使用这样的子选择来做到这一点:
select rj.id,
rj.stops,
sum(rd.time_elapsed) as total_time,
sum(rd.drive_distance) as total_distance
from routes_directions rd
right join (select r.id,
array_agg(j.stop_id) as stops
from routes r
left join routes_stops_join j
on r.id = j.route_id
group by r.id) rj
on rj.id = rd.route_id
group by rj.id, rj.stops;
但我想知道是否有一种方法可以在没有子选择的单个查询中执行此操作。
答案 0 :(得分:1)
只要此查询返回您需要的99%信息:
select rd.id,
sum(rd.time_elapsed) as total_time,
sum(rd.drive_distance) as total_distance
from routes_directions rd
group by rd.id;
我建议使用子查询或CTE,但使用LEFT JOIN而不是RIGHT JOIN。
create table routes(id int); insert into routes values (1),(2); create table routes_stops(route_id int, stop_id int); insert into routes_stops values (1,1),(1,2),(2,1),(2,3),(2,4); create table routes_directions(route_id int, dir_id int, time_elapsed int, drive_distance int); insert into routes_directions values (1,1,100,40),(1,2,60,60),(2,1,15,14),(2,3,20,30);
select rj.id, rj.stops, sum(rd.time_elapsed) as total_time, sum(rd.drive_distance) as total_distance from routes_directions rd left join (select r.id, array_agg(j.stop_id) as stops from routes r left join routes_stops j on r.id = j.route_id group by r.id) rj on rj.id = rd.route_id group by rj.id, rj.stops;
id | stops | total_time | total_distance -: | :------ | ---------: | -------------: 2 | {1,3,4} | 35 | 44 1 | {1,2} | 160 | 100
with stp as ( select r.id, array_agg(j.stop_id) as stops from routes r left join routes_stops j on r.id = j.route_id group by r.id ) select rd.route_id, stp.stops, sum(rd.time_elapsed) as total_time, sum(rd.drive_distance) as total_distance from routes_directions rd left join stp on stp.id = rd.route_id group by rd.route_id, stp.stops;
route_id | stops | total_time | total_distance -------: | :------ | ---------: | -------------: 1 | {1,2} | 160 | 100 2 | {1,3,4} | 35 | 44
dbfiddle here