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 634779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:19:58+00:00 2026-05-13T20:19:58+00:00

I have a SQL table containing train schedules. The table looks something like this:

  • 0

I have a SQL table containing train schedules. The table looks something like this:

Schedule

TrainNumber
LegID
DepartureTime
DepartureStation
ArrivalTime
ArrivalStation

My real database contain several tables, but for this question only the one above is relevant. Different trainNumber can have different amount of legs. Based on a departure station chosen by a user, I want to output all upcoming routes from that station.

The output must contain Departure time and Arrival station. But I don’t want to include the legs in between. Can anyone guide me in the right direction on how I can achieve this? I tried using a max statement. But didn’t quite get it to work the way I wanted to.

Also, there can be multiple departures by the same train number on the same day.

  • 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-05-13T20:19:58+00:00Added an answer on May 13, 2026 at 8:19 pm

    You would need to use the combination (DepartureTime + TrainNumber) as the key to your query, get the maximum arrival time given that combination of values, and then find out what the corresponding ArrivalStation is. So you could do an inner join between the Schedule and a grouped version of itself, i.e.

    SELECT
        TrainTableA.TrainNumber
        ,TrainTableA.DepartureTime
        ,ArrivalStation
    FROM
        (SELECT /* all upcoming train routes for given station */
            TrainNumber
            ,DepartureTime
            ,ArrivalTime
            ,ArrivalStation
        FROM
            Schedule
        WHERE DepartureStation = givenStation
        ) as TrainTableA
        INNER JOIN 
        (SELECT /* Just the last station for each departure */
            TrainNumber
            ,DepartureTime
            ,Max(ArrivalTime) as a
        FROM
            Schedule
        GROUP BY
              TrainNumber
              ,DepartureTime
        ) as TrainTableB
        ON
        TrainTableA.TrainNumber = TrainTableB.TrainNumber
        AND TrainTableA.DepartureTime = TrainTableB.DepartureTime
        AND TrainTableA.ArrivalTime = TrainTableB.a
    

    I can’t quite tell from the question if you have a universal indicator of the route sequence, so I used max(ArrivalTime). You could also use max(LegID) if each LegID is greater than the one before it. Also, I assumed that The ArrivalTime includes the date, so 1:00 AM on the next day is still later than 10:00 PM on the same day. So, of course, adjust to taste.

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

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have set up a simple stress test, when I… May 16, 2026 at 12:31 pm
  • Editorial Team
    Editorial Team added an answer Your question doesn't make it clear whether you're trying to… May 16, 2026 at 12:31 pm
  • Editorial Team
    Editorial Team added an answer It seems like the comments on the question have already… May 16, 2026 at 12:31 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

i have a sql table full of address' like this Hattie:07903 000066::Dad:07854 000095::Tom:07903 000044::Allistair:07765
We have a SQL Server table containing Company Name, Address, and Contact name (among
I have a SQL Server 2000 with a table containing an image column. How
I have a table containing hierarchical data. There are currently ~8 levels in this
This one is nasty complicated to solve. I have a table containing date ranges,
I have a table containing two columns in SQL that I want to extract
I have a table containing prices for a lot of different things in a
I have a SQL Table Roles in DB. I want to find out if
I have a 'text' type in a SQL table (BigNote), and a new nvarchar(2000)
I have data in the SQL 2005 table as below. Table Name: FilterData CategoryID

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.