Avatar of kishan66
kishan66
Flag for United States of America asked on

T-SQL (Sql Server 2008) to output rows to columns by joinging base table

I have two tables…… RoleMasterTable & TeamTable … and I want to combine them to third table TeamRoleTable using sql (Sql Server 2008)
... pls let me know how do i do that

RoleMasterTable
RoleID          Role
1      Manager
2      Lead
3      Member
 
TeamTable
  MANAGER               LEAD                  Member
Chris John      Keith Silverman      MIchel Jim
Chris John      Keith Silverman      Nick Kanon
Chris John      Mark Weith      Tom Thomb
Chris John      Harry Poter      Zach Hassle

OutPut to TeamRoleTable should be ……

TeamRoleTable
RoleID            Name
1                    Chris John
2                  Keith Silverman
2                 Mark Weith
2                Harry Poter
3                 MIchel Jim
3      Nick Kanon
3      Tom Thomb
3      Zach Hassle



Thanks
Microsoft SQL Server 2008SQL

Avatar of undefined
Last Comment
kishan66

8/22/2022 - Mon
SOLUTION
ralmada

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
alpmoon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ralmada

Of course to insert them into another table:

insert TeamRoleTable
select       distinct
      RoleID,
      Name
from (
select       Manager as [1],
      Lead as [2],
      Member as [3]
from TeamTable
) o
unpivot (Name for RoleID in (1, 2, 3)) p 

Open in new window

kishan66

ASKER
That was fast.....
It works ..
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23