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

  • Home
  • SEARCH
  • 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 6806473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:42:54+00:00 2026-05-26T19:42:54+00:00

MS SQL: Table One: Routes –Table Two: Stops (Uses an int field [seq] to

  • 0

MS SQL:

Table One: Routes –Table Two: Stops (Uses an int field [seq] to get the sequence of stops, key field ID)

How can I join [routes] to the [stops] table and return only the first and last route into a single row?

Let’s say I do a simple join:

Route 1 | Stop 1
Route 1 | Stop 2
Route 1 | Stop 3
Route 2 | Stop 1
Route 2 | Stop 2

I want it to return:

Route 1 | Stop 1 | Stop 3
Route 2 | Stop 1 | Stop 2

I can join them and get the Min(Seq) and Max(Seq), but not sure how I can get the ID and not Seq. It’s almost like I want to do two joins, but multiple fields. I need it to be efficient because of returning thousands of routes at a given time. So I don’t think a table valued function to get the stops would be beneficial. However, if I could get the min and max seq, returning the associated stop ID, I could simply join Stops to these to get the related data.

  • 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-26T19:42:54+00:00Added an answer on May 26, 2026 at 7:42 pm
    ;WITH S
         AS (SELECT *,
                    ROW_NUMBER() OVER (PARTITION BY RouteId ORDER BY seq) AS RN1,
                    ROW_NUMBER() OVER (PARTITION BY RouteId ORDER BY seq DESC) AS RN2
             FROM   Stops)
    SELECT R.Route,
           S1.Stop,
           S2.Stop
    FROM   Routes R
           JOIN S S1
             ON R.Id = S1.RouteId
           JOIN S S2
             ON R.Id = S2.RouteId
    WHERE  S1.RN1 = 1
           AND S2.RN2 = 1  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would one write a sql server DDL script that can: For each table
In Oracle SQL Developer, one can list the data in a table using the
I have a SQL table with some normal fields and one xml-type field. I
table one: field one field two 1 1-1 2 1-2 3 1-5 4 1-3
HI all, i have one sql table and field for that table is id
I have a SQL table that will be be joined with two subtables. One
I have one weird requirement. In a SQL table, I have two columns of
In SQL Server 2008: I have one table, and I want to do something
I have a SQL Mobile database with one table. It has several columns with
I am importing a table from one database to another in SQL Server 2008

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.