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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:54:00+00:00 2026-05-18T21:54:00+00:00

I have two tables: T1: | M_ID | P_ID1 | P_ID2 | rest of

  • 0

I have two tables:
T1:

| M_ID | P_ID1 | P_ID2 | rest of T1 columns |
|  0   |   0   |   1   |         ...        |
|  1   |   2   |   3   |         ...        |

T2:

| P_ID | Type | A | B |
|  0   |   1  | a | e |
|  1   |   2  | b | f |
|  2   |   1  | c | g |
|  3   |   2  | d | h |

Now, I want to have a query that selects this:

| M_ID | P_1a | P_1b | P_2a | P_2b | rest of T1 columns |
|  0   |  a   |  e   |  b   |  f   |         ...        |
|  1   |  c   |  g   |  c   |  h   |         ...        |

So, in words: I want to select all columns from T1, but I want to replace P_ID1 with the columns from T2, where the P_ID is equal to P_ID1, and the type is 1, and basically the same for P_ID2.

I can obviously get the information I need with multiple queries, but I was wondering if there is a way that I can do this with one query. Any ideas?

I’m currently using SQL Server 2008r2, but I’d also be interested in solutions for other database software.

Thanks for the help!

  • 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-18T21:54:00+00:00Added an answer on May 18, 2026 at 9:54 pm

    Sure, you just need to use a join:

    select T1.M_ID, t2_1.A as P_1a, t2_1.B as P_1b, t2_2.A as P_2a, t2_2.B as P_2b, ...
    from T1, T2 t2_1, T2 t2_2
    where T1.P_ID1 = t2_1.P_ID and T1.P_ID2 = t2_2.P_ID 
    

    basically we are joining T1 onto T2 twice, once for the P_1 values and a second time for the P_2 values. You need to alias T2 when you join it twice to distinguish between the two (that’s what the t2_1 and t2_2 are – a means of distinguishing between the two instances of the joined T2).

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

Sidebar

Related Questions

I have two tables, Tasks and TaskMilestones. I want a query to return the
I have two tables that contain completely separate information: table: tires columns: Tire_id name
I have a two mysql tables that I want to merge together in one
I'm having some problems trying to perform a query. I have two tables, one
I have two tables players and scores . I want to generate a report
I have two tables in an existing (MSSQL 2008 R2) database that are related
I have the two following tables: Owner: O_ID P_ID Bob Sam Steve Rex Pets
I have two queries that are basically the same: OLD TRANSACTIONS QUERY SELECT t.payment_method,
I have two tables that contain the following information models id manufactor 1 Mercedes
I have two tables, one that stores the current price, and one that stores

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.