比较两组SQL"GROUP BY"结果

5 sql t-sql

这是我的表:

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
Run Code Online (Sandbox Code Playgroud)

我想要做的是找到哪些路线通过相同的站但没有相同的站点.

例如:我们看到了

Route R1 passes through stations S1->S2->S3->S4->S5 
Route R2 passes through stations S1->S2->S3->S4->S5 
Run Code Online (Sandbox Code Playgroud)

但他们有不同的停留,所以结果应该是:

R1 
R2
Run Code Online (Sandbox Code Playgroud)

我想先将所有路线分组,并将属于该组的STATIONS与其他所有路线进行比较,并检查它们是否至少有一个不同的停靠点.

Lau*_*nce 0

我假设一个 TrainRoutes 表,其中 R1、R2 等各一行。如果需要,您可以将其替换为从 Stops 中选择不同的 RouteID。

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)
Run Code Online (Sandbox Code Playgroud)

您也可以在没有 TrainRoute 表的情况下执行此操作,如下所示,但您现在交叉连接两个较大的表:

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)
Run Code Online (Sandbox Code Playgroud)

http://sqlfiddle.com/#!6/76978/8