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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:55:37+00:00 2026-06-01T02:55:37+00:00

Im working in an SQL Query like this: (sorted by the station visits) TRAIN_ID

  • 0

Im working in an SQL Query like this: (sorted by the station visits)

TRAIN_ID   TYPE   STATION
111        'KC'   New York
111        'KC'   Washington
111        'KC'   Boston
111        'KC'   Denver
222        'FC'   London
222        'FC'   Paris

I’d like to SELECT distinct trains, and actual row must include the first and the last station like:

TRAIN_ID    TYPE     FIRSTSTATION    LASTSTATION
111         'KC'     New York        Denver
222         'FC'     Denver          Paris

Anyone can give a hand? Thank you in anticipation!

  • 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-01T02:55:39+00:00Added an answer on June 1, 2026 at 2:55 am

    Assuming you find something to define an order on the stations so that you can identify the “last” and “first” one, the following should work:

    WITH numbered_stations AS (
      SELECT train_id, 
             type, 
             row_number() over (partition by train_id order by some_order_column) as rn,
             count(*) over (partition by train_id) as total_stations
      FROM the_unknown_table
    )
    SELECT f.train_id,  
          f.type, 
          f.station as first_station, 
          l.station as last_station
    FROM (SELECT train_id, 
             type
             station
      FROM numbered_stations
      WHERE rn = 1
    ) f
      JOIN (SELECT train_id, 
                   type, 
                  station
            FROM numbered_stations 
            WHERE rn = total_stations) l 
      ON f.train_id = l.train_id
    ORDER BY train_id
    

    This assumes that some_order_column can be used to identify the last and first station.

    It also assumes that the type is always the same for all combinations of train_id and station.

    The shown syntax is standard ANSI SQL and should work on most modern DBMS.

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

Sidebar

Related Questions

I have query in SQL SERVER 2008 like this. And this is working. SELECT
I was executing sql query in zend something like this and it was working:
I am working on a simple SQL select like query parser and I need
I'm unable to convert this SQL query into a working linq statement select sum(cena),
I have a SQL query running using something like this: PreparedStatement pstmt = dbConn.prepareStatement(sqlQuery);
I have a select SQL query which use parallelism, something like this INSERT/*+ APPEND
I'm working on creating a SQL query that will pull records from a table
I am working on .aspx page that uses a t-sql query that uses a
I'm very interesting in inexpensive (better free) SQL query tool for working with multiple
I am working with SQL Server 2000, and trying to change the data type

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.