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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:42:40+00:00 2026-05-25T12:42:40+00:00

I am trying to do a SQL select on a table based on two

  • 0

I am trying to do a SQL select on a table based on two columns, but not in the usual way where the combination of values in both columns must be unique; I want to select where the value can only appear once in either column.
Given the dataset:

|pkid | fkself | otherData |
|-----+--------+-----------|
|  1  |   4    |   there   |
|  4  |   1    |    will   |
|  3  |   6    |     be    |
|  2  |   5    |    other  |
|  5  |   2    |   data    |
|  6  |   3    |  columns  |

I need to return either

|pkid | fkself | otherData |
|-----+--------+-----------|
|  1  |   4    |   there   |
|  3  |   6    |     be    |
|  2  |   5    |    other  |

or

|pkid | fkself | otherData |
|-----+--------+-----------|
|  4  |   1    |    will   |
|  5  |   2    |   data    |
|  6  |   3    |  columns  |

The only way I can think of to do this is to concatenate `pkid and fkid in order so that both row 1 and row 2 would concatenate to 1,4, but I’m not sure how to do that, or if it is even possible.

The rows will have other data columns, but it does not matter which row I get, only that I get each ID only once, whether the value is in pkid or fkself.

  • 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-25T12:42:40+00:00Added an answer on May 25, 2026 at 12:42 pm

    You can use least and greatest to get the smallest or biggest value of the two. That allows you to put them in the right order to generate those keys for you. You could concatenate the values as you suggested, but it’s not needed in this solution. With dense_rank you can generate a sequence for each of those fictional keys. Then, you can get the first OtherData from that sequence.

    select
      pkid,
      fkself,
      otherData
    from
      (select
        pkid,
        fkself,
        otherData,
        dense_rank() over (partition by least(pkid, fkself), greatest(pkid, fkself) order by pkid) as rank
      from
        YourTable t)
    where
      rank = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to fetch records based on two dates from sql server... Select
I'm trying to select more than 80,000 record in SQL Server in a table
I'm trying to use LINQ to SQL to select a few specific columns from
I'm trying to order the values in a related table using LINQ to SQL
I would like to update multiple columns in one table based on values in
I'm trying to migrate a MySQL-based app over to Microsoft SQL Server 2005 (not
I am trying to use PHP and MySQL's Create Table Select between two different
I'm trying to do the below sql statement in GORM select * from table1
What I'm trying to do is run the same SQL select on many Oracle
I am trying to execute this SQL command: SELECT page.page_namespace, pagelinks.pl_namespace, COUNT(*) FROM page,

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.