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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:01:07+00:00 2026-06-03T01:01:07+00:00

For this two tables: CREATE TABLE TEST1 ( ID INTEGER ,key VARCHAR(50),VALUE1 VARCHAR(50)); CREATE

  • 0

For this two tables:

CREATE TABLE TEST1 ( ID  INTEGER ,key  VARCHAR(50),VALUE1 VARCHAR(50));
CREATE TABLE TEST2 ( ID  INTEGER ,key  VARCHAR(50),VALUE2 VARCHAR(50));

Table1:

130,    'HANSEN',   'STREET1'
130,    'HANSEN',   'STREET2'
130,    'HANSEN',   'STREET3'
120,    'HANSEN',   'STREET5'
120,    'HANSEN',   'STREET6'

Table2:

130,    'HANSEN',   'ZIP1'
130,    'HANSEN',   'ZIP2'
130,    'HANSEN',   'ZIP3'
120,    'HANSEN',   'ZIP4'
120,    'HANSEN',   'ZIP5'

Is it possible to get the output as:

130,    'HANSEN',   'ZIP1','STREET1'
130,    'HANSEN',   'ZIP2','STREET2'
130,    'HANSEN',   'ZIP3','STREET3'
120,    'HANSEN',   'ZIP4','STREET5'
120,    'HANSEN',   'ZIP5','STREET6'

Tried with inner join on id,key but i get the Cartesian product

Thanks for your help.

Helper script (if someone is willing to test it):

DROP TABLE TEST1;
DROP TABLE TEST2;

CREATE TABLE TEST1 ( ID  INTEGER ,key  VARCHAR(50),VALUE1 VARCHAR(50));
CREATE TABLE TEST2 ( ID  INTEGER ,key  VARCHAR(50),VALUE2 VARCHAR(50));

INSERT INTO TEST1 VALUES (130,  'HANSEN',   'STREET1');
INSERT INTO TEST1 VALUES (130,  'HANSEN',   'STREET2');
INSERT INTO TEST1 VALUES (130,  'HANSEN',   'STREET3');
INSERT INTO TEST1 VALUES (120,  'HANSEN',   'STREET5');
INSERT INTO TEST1 VALUES (120,  'HANSEN',   'STREET6');

INSERT INTO TEST2 VALUES (130,  'HANSEN',   'ZIP1');
INSERT INTO TEST2 VALUES (130,  'HANSEN',   'ZIP2');
INSERT INTO TEST2 VALUES (130,  'HANSEN',   'ZIP3');
INSERT INTO TEST2 VALUES (120,  'HANSEN',   'ZIP4');
INSERT INTO TEST2 VALUES (120,  'HANSEN',   'ZIP5');
  • 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-03T01:01:11+00:00Added an answer on June 3, 2026 at 1:01 am

    This should do it:

    with t1 as (
        select id, 
               key, 
               value1,
               row_number() over (partition by id order by key, value1) as rn
        from test1
    ), 
    t2 as (
        select id, 
               key, 
               value2,
               row_number() over (partition by id order by key, value2) as rn
        from test2
    )
    select t1.id, 
           t2.key, 
           t2.value2||','||t1.value1
    from t1
      join t2 on t1.id = t2.id and t1.key = t2.key and t1.rn = t2.rn
    order by 1 desc, 3 asc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have create two tables with reference with another table: I like this: Table1:
I have two tables: This is table1: product_id|product_desc|product_name|product_s_desc This is table2: product_price_id|product_id|product_price Now I
My two tables look like this: TABLE1 TABLE2 +--------------------+ +--------------------+ |field1|field2|field3| and |field2|field4|field5| +--------------------+
I have two tables which looks something like this Table Queue int ID; string
I have two tables Department and Employee . Department table looks like this: ID
I have the following two tables: CREATE TABLE Test_Main ( [ID] INT IDENTITY ,
I have next input data, There are two tables create table Courses( id number(19)
I have two mysql tables: /* Table users */ CREATE TABLE IF NOT EXISTS
I'm working on a SQL Server 2008 database. This database has two tables. Book
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)

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.