Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:45:35+00:00 2026-06-14T12:45:35+00:00

Here is my table: ROUTES = the route ID STATIONS = the station ID

  • 0

Here is my table:

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

What I am trying to do is to find which routes pass through the same stations but don’t have the same stops.

For example:
We see that

Route R1 passes through stations S1->S2->S3->S4->S5 
Route R2 passes through stations S1->S2->S3->S4->S5 

but they have different stops, so the result should be:

R1 
R2

I thought to group first all the ROUTES and compare the STATIONS belonging to that group with all the others and check if they have at least one different stop.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:45:37+00:00Added an answer on June 14, 2026 at 12:45 pm

    I assumed a TrainRoutes table with one row for each of R1, R2 etc. You could replace this with select distinct RouteID from Stops if required.

    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)
    

    You can also do this without the TrainRoute table like so, but you’re now cross joining two larger tables:

    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)
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the XML: <Routes> <Route type=source> <Table> <Tablename>incoming</Tablename> <Fields> <Fieldsname ref=description name=Route Name>description</Fieldsname>
Here is the XML: <Routes> <Route type=source> <Name>incoming</Name> <Table><Tablename>incoming</Tablename></Table> </Route> <Route type=dest> <Name>outgoing</Name> </Route>
Here is my table in the dataset: SELECT tag_work_field.* FROM tag_work_field I created this
iam having controller like below which is not registered in routes table public class
I've got three tables which all join into one table. Here is the code:
## Sample Table ## Here are some particulars concerning proposed problem. ROWID = _id;
Lets say we have a table here, populated with the following data: acc_id1 acc_id2
i m having following type of simple sql server table Here I want to
http://logging.apache.org/log4net/release/config-examples.html Given the log table here: CREATE TABLE [dbo].[Log] ( [Id] [int] IDENTITY (1,
Here is my table in comma-delimited form: date, number 2010-09-02, 2 2010-10-01, 3 2011-01-01,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.