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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:43:00+00:00 2026-06-15T02:43:00+00:00

I am trying to join three tables in SQL Server 2008 R2, where I

  • 0

I am trying to join three tables in SQL Server 2008 R2, where I want the items in the second table to be added as new column.

To explain in detail – I have 3 tables:

First table contains User Name and User ID

UserID UserName
1       Mike
2       John
3       George

Second Table is position ID’s with Position Names

PositionID PositionName
1          RW
2          LW
3          DF
4          MDF
5          SS
6          CF
etc

Third table table contains their preferred positions where one user can have more than one

UserID  PositionId
1        1
1        3
2        2
2        3
2        5
3        2
3        7

When I join these tables I want to get single row for every user with all the preferred positions like

UserID   UserName  PreferedPosition  PreferedPosition2 PreferedPosition3
1        Mike      RW                LW               
2        John      CMF               SS                CF
3        George    LW                MDF

I don’t know how to achieve this, any help would be appreciated.

  • 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-15T02:43:02+00:00Added an answer on June 15, 2026 at 2:43 am

    If you have only a few numbers of positions, you can do it with PIVOT keyword

    select
        UserID,
        UserName,
        [1] as Position1,
        [2] as Position2,
        [3] as Position3
    from
    (
        select
            U.UserID, U.UserName, P.PositionName,
            row_number() over (partition by U.UserID order by P.PositionName) as RowNum
        from Positions_Users as PU
            inner join Positions as P on P.PositionID = PU.PositionID
            inner join Users as U on U.UserID = PU.UserID
    ) as P
        pivot
        (
            min(P.PositionName)
            for P.RowNum in ([1], [2], [3])
        ) as PIV
    

    SQL FIDDLE

    If, however, you want to have a dynamic number of columns, you have to use dynamic SQL, like this

    declare @stmt nvarchar(max), @stmt_columns1 nvarchar(max), @stmt_columns2 nvarchar(max)
    declare @Temp_Data table (RowNum nvarchar(max))
    
    insert into @Temp_Data
    select distinct row_number() over (partition by U.UserID order by P.PositionName) as RowNum
    from Positions_Users as PU
        inner join Positions as P on P.PositionID = PU.PositionID
        inner join Users as U on U.UserID = PU.UserID
    
    select @stmt_columns1 = stuff((select ', [' + RowNum + ']' from @Temp_Data for xml path(''), type).value('.', 'nvarchar(max)'), 1, 2, '')
    select @stmt_columns2 = stuff((select ', [' + RowNum + '] as Position' + RowNum from @Temp_Data for xml path(''), type).value('.', 'nvarchar(max)'), 1, 2, '')
    
    select @stmt = '
    select
        UserID,
        UserName,' + @stmt_columns2 + '
    from
    (
        select
            U.UserID, U.UserName, P.PositionName,
            row_number() over (partition by U.UserID order by P.PositionName) as RowNum
        from Positions_Users as PU
            inner join Positions as P on P.PositionID = PU.PositionID
            inner join Users as U on U.UserID = PU.UserID
    ) as P
        pivot
        (
            min(P.PositionName)
            for P.RowNum in (' + @stmt_columns1 + ')
        ) as PIV'
    
    exec sp_executesql @stmt = @stmt
    

    SQL FIDDLE

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

Sidebar

Related Questions

I have 2 tables I'm trying to join in SQL Server . Table one
I have the following three tables. I am trying to inter link (join) among
I am trying to execute a query on a table in my SQL Server
I'm running SQL Server 2008, and trying to run some queries on some poorly
In SQL Server 2005 I have encountered a table with unique ID column (with
I am using SQL Server 2008. I have 2 table variables like FirstName ==========
I have a SQL Server 2008 R2 database table with 12k address records where
I've inherited a system using SQL Server (2008 R2) with two tables that cover
SQL Server 2008. declare @pardate table ( pardateid int, pardatewhen datetime2(3) ) insert into
I want results from three tables where the results from the first table are

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.