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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:22:29+00:00 2026-06-08T06:22:29+00:00

I have a parent table with two foreign key references to a child table.

  • 0

I have a parent table with two foreign key references to a child table. I am trying to come up with optimal TSQL (SQL Server 2008) that will “pivot” the child column into a row. The table structure and needed result set structure are below. What is the best way to make this work?

Thanks.

set nocount on
declare @parent table ( parentid int , parentword varchar(3) , childid int , childotherid int )
insert into @parent values ( 0 , 'a' , 1 , 3 )
insert into @parent values ( 1 , 'b' , 2 , 4 )

declare @child table ( childid int , childword varchar(3) )
insert into @child values ( 1 , 'ppp' )
insert into @child values ( 2 , 'qqq' )
insert into @child values ( 3 , 'rrr' )
insert into @child values ( 4 , 'sss' )


needed result set
a       ppp     rrr
b       qqq     sss
  • 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-08T06:22:32+00:00Added an answer on June 8, 2026 at 6:22 am

    It is not pivoting.Join two times the child table with the parent table.See I have used the left join.If none of childid columns in parent tables are not null.Then you could use the inner join as well.

    select p.parentword,c1.childword,c2.childword from @parent p
    left join @child c1
    on p.childid = c1.childid
    left join @child c2
    on p.childotherid = c2.childid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, Parent and Child. The column Parent.favorite_child has a foreign key
I have two tables Loan(Parent Table) and Receipt(Child table)what i want to do is
I have a table with two child tables. For each record in the parent
I have two tables a parent and a child table. The child table has
I have two tables like Parent-Child. In Parent table, there are 7211 records. In
I have two tables linked by a foreign key. Example: CREATE TABLE one (id
I have two tables connected with one to many relationship. Parent Table is a
I have implemented inheritance for two parent classes called Table and Field . Each
If I have a parent table and a child table where there can be
I have a simple parent/child tables. Contract is the parent table. Each contract can

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.