Sql ISNULL(ct.s.),“+”,“+ISNULL(c.s.),作为nvarchar(100))作为s, 连续油管+连续油管,连续油管最大液位 从cte c连接c.id=ct.id+1和c.r=ct.r上的cte2 ct ) 选择c.r、c.s、c.st 从cte2 c连接c.s上的cte2 ct=ct.s和c.r ct.r和c.st ct.st 其中c.id=c.max\u水平,ct.id=ct.max\u水平

Sql ISNULL(ct.s.),“+”,“+ISNULL(c.s.),作为nvarchar(100))作为s, 连续油管+连续油管,连续油管最大液位 从cte c连接c.id=ct.id+1和c.r=ct.r上的cte2 ct ) 选择c.r、c.s、c.st 从cte2 c连接c.s上的cte2 ct=ct.s和c.r ct.r和c.st ct.st 其中c.id=c.max\u水平,ct.id=ct.max\u水平,sql,tsql,Sql,Tsql,演示只需搜索不带余数或精确关系除法的关系除法。我假设必须考虑连续顺序。但是订单在哪里?我认为,如果一列火车(或任何东西)从伦敦开往巴黎或从巴黎开往伦敦,情况会有所不同。表中有一个订单字段(我没有把它放在这里),但是您不必担心订单,因为我想找到经过相同车站但没有相同车站的路线。如果有人需要sql提琴(我没有时间了):事实上,这是个好问题。顺序重要吗?换句话说,如果路线1停在A、B、C,路线2停在A、C、B怎么办?这是一场比赛吗?还是他们在铁路线上,所以车站总是按顺序排列?@Beth,不,顺序没关系

演示

只需搜索不带余数或精确关系除法的关系除法。

我假设必须考虑连续顺序。但是订单在哪里?我认为,如果一列火车(或任何东西)从伦敦开往巴黎或从巴黎开往伦敦,情况会有所不同。表中有一个订单字段(我没有把它放在这里),但是您不必担心订单,因为我想找到经过相同车站但没有相同车站的路线。如果有人需要sql提琴(我没有时间了):事实上,这是个好问题。顺序重要吗?换句话说,如果路线1停在A、B、C,路线2停在A、C、B怎么办?这是一场比赛吗?还是他们在铁路线上,所以车站总是按顺序排列?@Beth,不,顺序没关系,不是应该是compB_NumStations=两个站吗?@Beth。这种情况是多余的。如果你有“a=b”和“a=c”,那么后面就是“a=c”。事实上,其中任何两个条件都足以满足相等条件。(也就是说,为了清楚起见,最好将这三个条件都放进去。)在您的交叉路线查询中,'=0或tblRoutes.STOPS=0'不应该是=1和tblRoutes.STOPS=1吗?@Beth 1表示列车在车站停车,0-当它不在车站停车时,据我所知,问题是让根穿过相同的站点,但不要停在它们上面。对不起,看了你的sqlFiddle之后,我想不出你在做什么。最终的结果是R1和R2不应该停在同一个站点,但中途有不同的站点吗?谢谢,伙计,我尝试了你的解决方案,效果很好。谢谢
ROUTES = the route ID
STATIONS = the station ID
STOPS? = if the train stops at this station then is equal to 1 otherwise 0

-------------------------
ROUTES STATIONS  STOPS?
-------------------------
R1    S1    1
R1    S2    0
R1    S3    1
R1    S4    0
R1    S5    1
R2    S1    1
R2    S2    1
R2    S3    1
R2    S4    0
R2    S5    1
R3    S1    1
R3    S2    0
R3    S4    1
R3    S5    0
R3    S6    1
R3    S7    1
R4    S1    1
R4    S2    1
R4    S3    0
R4    S4    1
R5    S2    1
R5    S3    0
R5    S4    1
Route R1 passes through stations S1->S2->S3->S4->S5 
Route R2 passes through stations S1->S2->S3->S4->S5 
R1 
R2
  SELECT DISTINCT
    tblRoutes.ROUTES
   ,tblRoutesCross.ROUTES CrossingRoute
   ,tblRoutes.STATIONS
  FROM
    tblRoutes
  INNER JOIN
    tblRoutes tblRoutesCross
  ON
    tblRoutesCross.STATIONS = tblRoutes.STATIONS
  AND
    tblRoutes.ROUTES < tblRoutesCross.ROUTES
  AND
    (
       tblRoutesCross.STOPS = 0 
      OR
       tblRoutes.STOPS = 0 
    )
 SELECT DISTINCT
    tblRoutes.ROUTES
   ,tblRoutesCross.ROUTES CrossingRoute
   --,tblRoutes.STATIONS
  FROM
    tblRoutes
  INNER JOIN
    tblRoutes tblRoutesCross
  ON
    tblRoutesCross.STATIONS = tblRoutes.STATIONS
  AND
    tblRoutes.ROUTES < tblRoutesCross.ROUTES
  AND
    (
       tblRoutesCross.STOPS = 0 
      OR
       tblRoutes.STOPS = 0 
    )
  SELECT DISTINCT
    tblRoutes.ROUTES
   ,tblRoutesCross.ROUTES CrossingRoute
   --,tblRoutes.STATIONS
  FROM
    tblRoutes
  INNER JOIN
    tblRoutes tblRoutesCross
  ON
    tblRoutesCross.STATIONS = tblRoutes.STATIONS
  AND
    tblRoutes.ROUTES < tblRoutesCross.ROUTES
  AND
    (
       tblRoutesCross.STOPS = 0 
      AND
       tblRoutes.STOPS = 0 
    )
DECLARE @table TABLE (r NVARCHAR(3), s NVARCHAR(3), st BIT)
DECLARE @temp TABLE (r NVARCHAR(3), s NVARCHAR(100), st NVARCHAR(100))

INSERT INTO @table
        ( [r], [s], [st] )
VALUES  ('R1','S1',1 ),
        ('R1','S2',0),
        ('R1','S3',1),
        ('R1','S4',0),
        ('R1','S5',1),
        ('R2','S1',1),
        ('R2','S2',1),
        ('R2','S3',1),
        ('R2','S4',0),
        ('R2','S5',1),
        ('R3','S1',1),
        ('R3','S2',0),
        ('R3','S4',1),
        ('R3','S5',0),
        ('R3','S6',1),
        ('R3','S7',1),
        ('R4','S1',1),
        ('R4','S2',1),
        ('R4','S3',0),
        ('R4','S4',1),
        ('R5','S2',1),
        ('R5','S3',0),
        ('R5','S4',1)

SELECT * FROM @table

INSERT INTO @temp
        ( [r], [s], [st] )

SELECT DISTINCT r ,(
Select s + ',' AS 'data()' 
            From @table R2
            WHERE R2.[r] = [R].r
            ORDER BY [r]
            For XML PATH ('') ) [Routs]
            , (
Select CAST([st]AS NVARCHAR(2)) + ',' AS 'data()' 
            From @table R2
            WHERE R2.[r] = [R].r
            ORDER BY [r]
            For XML PATH ('') ) [Stops]
    FROM @table R

SELECT * FROM @temp

SELECT * FROM @temp WHERE [s] IN (SELECT [s] FROM @temp GROUP BY s HAVING COUNT(*) > 1 )

SELECT T.* FROM @temp T
INNER JOIN (
SELECT [s], [st] FROM @temp WHERE [r] IN (
 SELECT [r] FROM @temp WHERE [s] IN 
    ( SELECT [s] FROM @temp GROUP BY s HAVING COUNT(*) > 1 ) )
    GROUP BY [s], [st] HAVING COUNT (*) = 1 ) X ON T.[s] = X.[s] AND T.[st] = X.[st]
Select
    r1.RouteID Route1,
    r2.RouteID Route2
From
    -- cross to compare each route with each route
    dbo.TrainRoutes r1
        Cross Join
    dbo.TrainRoutes r2
        Inner Join
    dbo.Stops s1
        On r1.RouteID = s1.RouteID
        Inner Join
    dbo.Stops s2
        On r2.RouteID = s2.RouteID
Where
    r1.RouteID < r2.RouteID -- no point in comparing R1 with R2 and R2 with R1
Group By
    r1.RouteID,
    r2.RouteID
Having
     -- check each route has the same number of stations
    count(Distinct s1.stationID) = count(Distinct s2.stationID) And
    -- check each route has the same stops
    Sum(Case When s1.StationID = s2.StationID Then 1 Else 0 End) = count(Distinct s1.StationID) And
    -- check each route has different halts
    sum(Case When s1.StationID = s2.StationID And s1.Halts = s2.Halts Then 1 Else 0 End) != count(Distinct s1.StationID)
Select
    s1.RouteID Route1,
    s2.RouteID Route2
From
    dbo.Stops s1
        Cross Join
    dbo.Stops s2
Where
    s1.RouteID < s2.RouteID
Group By
    s1.RouteID,
    s2.RouteID
Having
    count(Distinct s1.stationID) = count(Distinct s2.stationID) And
    Sum(Case When s1.StationID = s2.StationID Then 1 Else 0 End) = count(Distinct s1.StationID) And
    sum(Case When s1.StationID = s2.StationID And s1.Halts = s2.Halts Then 1 Else 0 End) != count(Distinct s1.StationID)
select compa_Route, compb_Route
from (select compa.route as compa_route, compb.route as compb_Route,
             MAX(compa.numstations) as compa_NumStations,
             MAX(compb.NumStations) as compb_NumStations,
             SUM(case when compa.stop <> compb.stop then 1 else 0 end) as DifferentStops,
             COUNT(*) as both_NumStations
      from (select t.*, COUNT(*) over (partition by route) as numstations
            from t
           ) routea join
           (select t.*, COUNT(*) over (partition by route) as numstations
            from t
           ) routeb
           on routea.station = routeb.station and
              routea.route < routeb.route
      group by compa.route, compb.route
     ) ab
where compa_NumStations = compb_NumStations and
      compa_NumStations = both_NumStations and
      DifferentStops > 0
;WITH cte (id, r, s, st, max_level) AS
 (
  SELECT ROW_NUMBER() OVER(PARTITION BY t.r ORDER BY (SELECT 1)) as id,
         t.r, t.s, t.st,
         COUNT(*) OVER(PARTITION BY t.r) AS max_level
  FROM dbo.test11 t
  ), cte2 (id, r, s, st, max_level) AS
 (
  SELECT id, r, s, st, max_level
  FROM cte
  WHERE id = 1
  UNION ALL
  SELECT c.id, c.r, CAST(ISNULL(ct.s, '') + ', ' + ISNULL(c.s, '') AS nvarchar(100)) AS s,
         ct.st + c.st, c.max_level
  FROM cte c JOIN cte2 ct ON c.id = ct.id + 1 AND c.r = ct.r
)
SELECT c.r, c.s, c.st
FROM cte2 c JOIN cte2 ct ON c.s = ct.s AND c.r <> ct.r AND c.st <> ct.st
WHERE c.id = c.max_level AND ct.id = ct.max_level