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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:27:24+00:00 2026-05-23T16:27:24+00:00

Table A : id members ____________ 1 a 2 b 3 c 4 c

  • 0

Table A :

id  members      
____________
1    a
2    b
3    c
4    c
4    d
5    e

Table B

id  countries
_____________
1   us
1   europe
1   australia
3   india
3   china
4   australia
4   canada
5   mexico

Result should look like

id   members   countries
___________________________
1     a        us
1              europe
1              australia
2     b
3     c        india
3              china
4     c        australia
4     d        cananda
5     e         mexico

I am sorry if I am editing at wrong place.

@Martin Thanks for your reply. It works great for 2 tables. But actually I am trying to combine more than 2. Let’s say I have another table C

id     prime_members
---------------------
1       p1
1       p2

I need the result to look like

id   members   countries     prime_mem
___________________________________________
1     a        us            p1
1              europe        p2
1              australia
2     b
3     c        india
3              china
4     c        australia
4     d        cananda
5     e         mexico

so I tried the query like this

 ;WITH A AS 
( SELECT id, members,
 ROW_NUMBER() OVER (PARTITION BY id ORDER BY members) AS RN FROM @A ),
  B AS 
  ( SELECT id, countries, 
  ROW_NUMBER() OVER (PARTITION BY id ORDER BY countries) AS RN FROM @B ),
   C AS 
  ( SELECT id,PRIME_MEM,
  ROW_NUMBER() OVER (PARTITION BY id ORDER BY PRIME_MEM) AS RN FROM @C )

   SELECT COALESCE(A.id,B.id,C.ID) AS id,  A.members, B.countries,C.PRIME_MEM
    FROM A FULL OUTER JOIN B on A.id = B.id AND A.RN=B.RN
    FULL OUTER JOIN C  ON A.ID =C.ID AND C.RN = A.RN 

then I got result like this:

id   members   countries     prime_mem
___________________________________________
1     a        us            p1
1              europe        
1              australia
1                            p2
2     b
3     c        india
3              china
4     c        australia
4     d        cananda
5              mexico

I figured it out that C.RN=B.RN gives correct output because A.RN does not have RN>1 as it has only 1 row. Here it is easy because only 3 tables and few rows. But in real time how to figure out and solve this problem. Also Table A is a reference table, i.e. values for ID in table B and C will be present in table A.

Any help is appreciated.
Thanks.

  • 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-23T16:27:25+00:00Added an answer on May 23, 2026 at 4:27 pm

    There is no “natural order” to tables so (unless there is an additional column such as id that you haven’t shown us) there is nothing that can be used to ensure that the order you have in your example source data will be preserved in the final output.

    ;WITH A AS
    (
    SELECT id,
           members, 
           ROW_NUMBER() OVER (PARTITION BY id ORDER BY members) AS RN
    FROM tableA
    ),
    B AS
    (
    SELECT id,
           countries, 
           ROW_NUMBER() OVER (PARTITION BY id ORDER BY countries) AS RN
    FROM tableB
    )
    SELECT COALESCE(A.id,B.id) AS id, 
           A.members,
           B.countries
    FROM A FULL OUTER JOIN B on A.id = B.id AND A.RN=B.RN
    

    Returns (Note china and india have swapped places as my answer orders alphabetically within each group)

    id          members countries
    ----------- ------- ---------
    1           a       australia
    1           NULL    europe
    1           NULL    us
    2           b       NULL
    3           c       china
    3           NULL    india
    4           c       australia
    4           d       canada
    5           NULL    mexico
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my members table i would like a summary of all different people in
The table that contains information about members has a structure like: id | fname
I'm using three mysql tables that looks like: The table for members accounts |
My database structure is like this: Members table |id|username|... Messages table |id|fromId|toId|content In the
I have five tables in my database. Members, items, comments, votes and countries. I
I have a members table in MySQL CREATE TABLE `members` ( `id` int(10) unsigned
I have a table that contains members names and a field with multiple ID
In this table how would I use YQL/Xpath to get all data members of
On the Members table are columns MemberID and PointsEarned. I want to update the
I have a database that has two tables, these tables look like this codes

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.