簡體   English   中英

合並SQL查詢的結果並進行所有組合

[英]Combine results from SQL query and take all combinations

我在SQL Server中有一個問題。 我有下表:

ROUTES = the route ID
STATIONS = the station ID
ORDER = the order the train pass from the stations
STOPS? = if the train stops at this station then is equal to 1 otherwise 0

樣本數據:

-----------------------------------------------------
ROUTES      STATIONS    ORDER       STOPS?
-----------------------------------------------------
R1          S1          1           1
R1          S2          2           1
R1          S3          3           1
R1          S4          4           1
R1          S5          5           1
R2          S2          1           1
R2          S3          2           1
R2          S4          3           1
R3          S1          1           1
R3          S2          2           1
R3          S7          3           1
R3          S4          4           1
R3          S5          5           1
R3          S6          6           1
R4          S1          1           1
R4          S2          2           1
R4          S3          3           0
R4          S4          4           1
R5          S2          1           1
R5          S3          2           0
R5          S4          3           1
R6          S3          1           1
R6          S4          2           0
R6          S5          3           0
R6          S6          4           1
R7          S2          1           1
R7          S3          2           0
R7          S4          3           0
R7          S5          4           1

因此,總結一下,我們有以下路線:

R1: S1-S2-S3-S4-S5
R2: S2-S3-S4
R3: S1-S2-S7-S4-S5-S6
R4: S1-S2-S3-S4
R5: S2-S3-S4
R6: S3-S4-S5-S6
R7: S2-S3-S4-S5

假設S2和S4是連接站

這意味着,如果從某條路線出發的火車在此停靠(STOPS = 1),則乘客可以下車並從另一條路線乘坐另一趟火車

所以我們有一張桌子提到連接站

conn_stations
--------------
S2
S4

我的問題是,如何獲得從站點S1出發並到達站點S5的所有可能路線組合。 乘客可以根據上述數據更改路線,我們應該得出以下結果(路線):

R1:     S1-S2-S3-S4-S5
R3:     S1-S2-S7-S4-S5
temp1:  S1-S2(from R1)-S7-S4-S5(from R3)
temp2:  S1-S2(from R3)-S3-S4(from R1)-S5(from R3)
temp3:  S1-S2-S3-S4(from R1)-S5(from R3)
e.t.c

希望您能理解您的要求。

如果有幫助,我可以使用一張表格說兩個工作站之間的距離,這也表明連接了哪些工作站

Station A   Station B   Distance
-------------------------------------
S1          S2          5
S2          S3          1
S2          S7          8
S3          S4          15
S4          S5          16
S5          S6          25
S7          S4          10

我認為,如果不必在連接站和終端站之間包含所有臨時站,會更容易,但是這是我對直接路線所做的操作:

select sroute, sstation, sorder, eroute, estation, eorder
from
 (select route as sroute, station as sstation, order as sorder
  from path
  where station = "s1" and stops = 1) pstart inner join
 (select route as eroute, station as estation, order as eorder
  from path
  where station = "s5" and stops = 1) pend on
 pstart.sroute = pend.eroute) direct

和通過一個連接站的路線:

SELECT sroute, sstation, sorder, 
        croute, cstation, corder,
        oroute, ostation, oorder,
        p.route , p.station, p.ORDER
FROM
    (SELECT sroute, sstation, sorder, 
                     croute, cstation, corder, 
                     p.route AS oroute, p.station AS ostation, p.ORDER AS oorder
    FROM
         (SELECT sroute, sstation, sorder, 
                 p.route AS croute, p.station AS cstation, p.ORDER AS corder
          FROM
              (SELECT route AS sroute, station AS sstation, ORDER AS sorder
               FROM path
               WHERE station = "s1" AND stops = 1
               ) pstart INNER JOIN
               path p ON 
               pstart.route = p.route INNER JOIN 
               conn_stations c ON 
               p.station = c.station
          WHERE p.stops = 1
          ) conn INNER JOIN 
          path p ON 
          conn.cstation = p.station
          WHERE p.stops = 1 AND p.route <> conn.route 
      ) conn_off INNER JOIN
        path p ON 
        conn_off.oroute = p.route
WHERE p.stops = 1 AND p.station = "s5"

但連接站的數量將反映在SQL中,因此最好使用編程工具。

高溫超導

我認為您的示例數據中有39條從S1S5路線(如果我的查詢正確的話)。 我不一定認為SQL是完成此工作的正確工具(尤其是根據注釋,如果路由變為雙向的話)。

查詢:

declare @StartStation char(2)
declare @EndStation char(2)
select @StartStation = 'S1'
select @EndStation = 'S5'

;With PartialRoutes as (
    select Route,Station,CONVERT(varchar(max),Station) as CurrentPath,RouteOrder,1 as HasTransferred,CONVERT(varchar(max),Route) as RoutesTaken,Stop
    from
        @Routes r
    where
        r.Station = @StartStation and r.Stop = 1
    union all
    --Continue on current route
    select pr.Route,r.Station,pr.CurrentPath + '-' + r.Station,r.RouteOrder,0,RoutesTaken,r.Stop
    from
        PartialRoutes pr
            inner join
        @Routes r
            on
                pr.Route = r.Route and
                pr.RouteOrder = r.RouteOrder - 1
    union all
    --Transfers
    select r.Route,r.Station,pr.CurrentPath,r.RouteOrder,1,RoutesTaken + '-' + r.Station + '(' + pr.Route + ',' + r.Route + ')',r.Stop
    from
        PartialRoutes pr
            inner join
        @Connections c
            on
                pr.Station = c.Station
            inner join
        @Routes r
            on
                pr.Route != r.Route and
                pr.Station = r.Station
    where
        pr.HasTransferred = 0 --Prevents us transferring multiple times in a single station
            and r.Stop = 1
)
select * from PartialRoutes where Station = @EndStation and Stop=1 option (MAXRECURSION 0)

結果:

Route Station CurrentPath         RouteOrder  HasTransferred RoutesTaken
----- ------- ------------------- ----------- -------------- --------------------------
R7    S5      S1-S2-S3-S4-S5      4           0              R4-S2(R4,R7)
R3    S5      S1-S2-S3-S4-S5      5           0              R4-S2(R4,R7)-S4(R7,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R4-S2(R4,R7)-S4(R7,R1)
R3    S5      S1-S2-S3-S4-S5      5           0              R4-S2(R4,R5)-S4(R5,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R4-S2(R4,R5)-S4(R5,R1)
R3    S5      S1-S2-S7-S4-S5      5           0              R4-S2(R4,R3)
R1    S5      S1-S2-S7-S4-S5      5           0              R4-S2(R4,R3)-S4(R3,R1)
R3    S5      S1-S2-S3-S4-S5      5           0              R4-S2(R4,R2)-S4(R2,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R4-S2(R4,R2)-S4(R2,R1)
R1    S5      S1-S2-S3-S4-S5      5           0              R4-S2(R4,R1)
R3    S5      S1-S2-S3-S4-S5      5           0              R4-S2(R4,R1)-S4(R1,R3)
R3    S5      S1-S2-S3-S4-S5      5           0              R4-S4(R4,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R4-S4(R4,R1)
R7    S5      S1-S2-S3-S4-S5      4           0              R3-S2(R3,R7)
R3    S5      S1-S2-S3-S4-S5      5           0              R3-S2(R3,R7)-S4(R7,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R3-S2(R3,R7)-S4(R7,R1)
R3    S5      S1-S2-S3-S4-S5      5           0              R3-S2(R3,R5)-S4(R5,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R3-S2(R3,R5)-S4(R5,R1)
R3    S5      S1-S2-S3-S4-S5      5           0              R3-S2(R3,R4)-S4(R4,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R3-S2(R3,R4)-S4(R4,R1)
R3    S5      S1-S2-S3-S4-S5      5           0              R3-S2(R3,R2)-S4(R2,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R3-S2(R3,R2)-S4(R2,R1)
R1    S5      S1-S2-S3-S4-S5      5           0              R3-S2(R3,R1)
R3    S5      S1-S2-S3-S4-S5      5           0              R3-S2(R3,R1)-S4(R1,R3)
R3    S5      S1-S2-S7-S4-S5      5           0              R3
R1    S5      S1-S2-S7-S4-S5      5           0              R3-S4(R3,R1)
R7    S5      S1-S2-S3-S4-S5      4           0              R1-S2(R1,R7)
R3    S5      S1-S2-S3-S4-S5      5           0              R1-S2(R1,R7)-S4(R7,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R1-S2(R1,R7)-S4(R7,R1)
R3    S5      S1-S2-S3-S4-S5      5           0              R1-S2(R1,R5)-S4(R5,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R1-S2(R1,R5)-S4(R5,R1)
R3    S5      S1-S2-S3-S4-S5      5           0              R1-S2(R1,R4)-S4(R4,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R1-S2(R1,R4)-S4(R4,R1)
R3    S5      S1-S2-S7-S4-S5      5           0              R1-S2(R1,R3)
R1    S5      S1-S2-S7-S4-S5      5           0              R1-S2(R1,R3)-S4(R3,R1)
R3    S5      S1-S2-S3-S4-S5      5           0              R1-S2(R1,R2)-S4(R2,R3)
R1    S5      S1-S2-S3-S4-S5      5           0              R1-S2(R1,R2)-S4(R2,R1)
R1    S5      S1-S2-S3-S4-S5      5           0              R1
R3    S5      S1-S2-S3-S4-S5      5           0              R1-S4(R1,R3)

我在末尾添加了一個列,顯示了遵循的路由,例如R4-S2(R4,R7)-S4(R7,R6)表示它們從路由R4開始,然后在S2轉移至路由R7 ,然后在S4轉移到路線R6

希望您可以接受此查詢並根據需要對其進行修改(即,我仍然不知道問題中最終表的相關性)。

編輯我在查詢上添加了一個最終條件,即火車需要在目標站處Stop 我不確定這是否是必需條件,但是無論如何它不會影響S1S5的結果。 退出很容易。


基於此數據設置:

declare @Routes table (Route char(2) not null, Station char(2) not null,RouteOrder int not null,Stop bit not null)
insert into @Routes (Route,Station,RouteOrder,Stop) values
('R1','S1',1,1),('R1','S2',2,1),('R1','S3',3,1),('R1','S4',4,1),('R1','S5',5,1),
('R2','S2',1,1),('R2','S3',2,1),('R2','S4',3,1),
('R3','S1',1,1),('R3','S2',2,1),('R3','S7',3,1),('R3','S4',4,1),('R3','S5',5,1),('R3','S6',6,1),
('R4','S1',1,1),('R4','S2',2,1),('R4','S3',3,0),('R4','S4',4,1),
('R5','S2',1,1),('R5','S3',2,0),('R5','S4',3,1),
('R6','S3',1,1),('R6','S4',2,0),('R6','S5',3,0),('R6','S6',4,1),
('R7','S2',1,1),('R7','S3',2,0),('R7','S4',3,0),('R7','S5',4,1)

declare @Connections table (Station char(2) not null)
insert into @Connections (Station) values ('S2'),('S4')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM