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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:30:31+00:00 2026-05-26T15:30:31+00:00

Let’s say that I have 2 columns: _______________ | id1 | id2 | |——|——|

  • 0

Let’s say that I have 2 columns:

_______________
| id1  |  id2 |
|------|------|
|  1   |   2  |
|  2   |   1  |
|  3   |   4  |
|  4   |   1  |
|  4   |   3  |
|  1   |   4  |
      ...

If I have a row id1 = 1 and id2 = 2, there will be a row somewhere after it which will be id1 = 2 and id2 = 1.

How can I get the top of each (id1, id2) pair?


Update:

The result from the example should be:

row1: 1 | 2
row2: 3 | 4
row3: 4 | 1
  • 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-26T15:30:32+00:00Added an answer on May 26, 2026 at 3:30 pm

    Linking back to the original table depends a lot on what is in that table that causes the order. Lets says you have a date field or a sequence field and you want the smallest value…

    In this example I assume seqField is unique by row

    Then it looks like this:

    --drop table #test
    
     SELECT 1 as seqField, 1 as id1, 2 as id2
     INTO #test
     UNION ALL
     SELECT 2,2,1
     UNION ALL
     SELECT 3,3,4
     UNION ALL
     SELECT 4,4,1
     UNION ALL
     SELECT 5,4,3
     UNION ALL
     SELECT 6,1,4 
     UNION ALL
     SELECT 7,10,20;
    
    WITH norm AS 
    ( 
       SELECT CASE WHEN id1 > id2 THEN id1 ELSE id2 END as a,
              CASE WHEN id1 < id2 THEN id1 ELSE id2 END as b, * 
       FROM #test
    ), setList AS
    (
      SELECT DISTINCT a, b, min(seqField) as s
      FROM norm
      GROUP BY a, b
    )
    SELECT #test.* 
    FROM #test
    JOIN setList ON #test.seqField = setList.s
    

    This gives

    seq id1 id2
    1   1   2
    3   3   4
    4   4   1
    7   10  20
    

    As sqlchan points out seqField can be replaced with %%physloc%% if you don’t have an existing column to use.

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

Sidebar

Related Questions

Let's say I'm creating an OpenGL game in C++ that will have many objects
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say after I had login I will be prompt to enter the Name
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say that I have a set of relations that looks like this: relations
Let's say that I have a binary that I am building, and I include
Let's say that I have classes like this: public class Parent { public int
Let's say i have this block of code, <div id=id1> This is some text
let's say that I have string: s = Tuple: and Tuple (stored in a

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.