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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:02:18+00:00 2026-06-17T07:02:18+00:00

Let me try to explain the scenario. I have two tables A (Columns –

  • 0

Let me try to explain the scenario. I have two tables A (Columns – A1, A2, A3) & B (Columns – B1, B2, B3). I need to join table A with table B on A1.B2. For every join, table B has one or two records with different values for B3(X or Y). I wanna write one query where the JOIN query needs to pick the row with B3=X(if there’s no other row with B3=Y); If two rows exists (B3=X & B3=Y), then the query needs to pick only the row with B3=Y (ignoring the row with B3=X).

Let me try to give some values to the tables & explain a little bit more.

Table A
********
A1   A2   A3
1    11   111
2    22   222
3    33   333
4    44   444

Table B
********
B1   B2   B3
6    1    X
7    1    Y
8    2    X
9    3    X
10   3    Y
11   4    X

Again.. JOIN is on A1.B2. The result should be as following,

JOIN Results
*************
A1   A2   A3   B1   B2   B3
1    11   111  7    1    Y
2    22   222  8    2    X
3    33   333  10   3    Y
4    44   444  11   4    X

Let me know if you guys have any clarification about my question.

Thanks in advance.
Yogi

  • 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-17T07:02:19+00:00Added an answer on June 17, 2026 at 7:02 am

    You can pick the rows from table B with the ROW_NUMBER function if you partition by the join column and order by your “picking order” column:

    SELECT b1, b2, b3,
           ROW_NUMBER() OVER (PARTITION BY b2 ORDER BY b3 DESC) as rn
      FROM b;
    
    1 Y 1
    1 X 2
    2 X 1
    3 Y 1
    3 X 2
    4 X 1
    

    Then you can filter the first row, the one with rn=1:

    SELECT b1, b2, b3
      FROM (SELECT b1, b2, b3,
                   ROW_NUMBER() OVER (PARTITION BY b2 ORDER BY b3 DESC) as rn
              FROM b)
     WHERE rn=1;
    
    7  1 Y
    8  2 X
    10 3 Y
    11 4 X
    

    The filtered rows can then be joined to table a:

    SELECT *
      FROM a 
      JOIN (
            SELECT b1, b2, b3
              FROM (SELECT b1, b2, b3,
                           ROW_NUMBER() OVER (PARTITION BY b2 ORDER BY b3 DESC) as rn
                      FROM b
                    )
              WHERE rn=1
           ) bfilter  ON a.a1 = bfilter.b2;
    
    1 11 111  7 1 Y
    2 22 222  8 2 X
    3 33 333 10 3 Y
    4 44 444 11 4 X   
    

    If ‘X’ and ‘Y’ are not actual values, you can extend the ORDER clause with a CASE statement to allow for general values:

     ROW_NUMBER() OVER (PARTITION BY b2 ORDER BY 
       CASE b3 WHEN 'Y' THEN 1
               WHEN 'X' THEN 2
               ...
        END ASC)
    

    Edit:

     SELECT a1, a2, a3, b1, b2, b3
       FROM (
             SELECT a1, a2, a3, b1, b2, b3,
                    ROWNUMBER() OVER (PARTITION BY a1 ORDER BY 
                       CASE WHEN a2=... AND b3=... THEN 1
                            WHEN a2=... AND b3=... THEN 2
                            ...
                       END ASC)
              FROM a JOIN b ON a.a1 = b.b2
            ) 
      WHERE rn = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me try to explain My case, I have two table and each of
Let me try to explain what I need. I have a server that is
Let me try and explain the scenario: I have a form with multiple items
Okay let me try to explain this.. So i have a player, that can
I got stuck with this scenario. Let me explain as follows. I have a
Let me try to explain this the best I can. I have a component
Let me try to explain what I have on view and what's I'm trying
Let me try and explain what I am talking about better. Main GUI has
Let me try to explain this to the best of my ability: I have
I have an incredibly weird request for help here, let me try to explain

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.