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

  • Home
  • SEARCH
  • 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 7574945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:33:45+00:00 2026-05-30T16:33:45+00:00

I have one user table. and second language table which contains language that user

  • 0

I have one user table.

enter image description here

and second language table which contains language that user can speak

enter image description here

and third passport table which contains user’s passport details(user can have multiple passport)

enter image description here

[I have 5 more table except these]

I want to convert these language and passport rows to columns into one result-set(having user details also) and in the result set it should show yes/no value for language and passport column for every user record depending on what language user can speak and what passport they have.Output would be as given below:

enter image description here

We can do this by using PIVOT.I tried with single table but how to do it for multiple tables.

Thanks

  • 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-30T16:33:46+00:00Added an answer on May 30, 2026 at 4:33 pm

    Maybe something like this:

    First the test data:

    CREATE TABLE #tblUser 
                (
                    userid INT, 
                    userName VARCHAR(100),
                    department VARCHAR(100)
                )
    CREATE TABLE #tblLanguage 
                (
                    uniqueid INT,
                    userid INT,
                    [language] VARCHAR(100)
                )
    CREATE TABLE #tblContryPassport
                (
                    uniqueid INT,
                    userid INT,
                    contryPassport VARCHAR(100)
                )
    INSERT INTO #tblUser
    VALUES
        (1,'aa','TT'),
        (2,'bb','gg'),
        (3,'cc','rr'),
        (4,'dd','dd')
    INSERT INTO #tblLanguage
    VALUES
        (1,1,'American'),
        (1,1,'Arabic'),
        (1,2,'Azerbaujani'),
        (1,3,'Bulgarian'),
        (1,3,'Chaochow'),
        (1,4,'Behdini')
    INSERT INTO #tblContryPassport
    VALUES
        (1,1,'Hongkong'),
        (1,1,'Malaysia'),
        (1,2,'Spain'),
        (1,3,'China'),
        (1,4,'India'),
        (1,4,'UK')
    

    Then concat the unique column for the pivot. If there is two then choose one of them:

    DECLARE @cols VARCHAR(MAX),
            @colsWithIsNull VARCHAR(MAX)
    ;WITH CTE
    AS
    (
        SELECT
            #tblContryPassport.contryPassport AS Name
        FROM
            #tblContryPassport
        UNION ALL
        SELECT
            #tblLanguage.[language] AS Name
        FROM
            #tblLanguage
    ), CTE2
    AS
    (
        SELECT
            ROW_Number() OVER(PARTITION BY name ORDER BY name) AS iRank,
            CTE.Name
        FROM
            CTE
    )
    SELECT  @cols = COALESCE(@cols + ','+QUOTENAME(Name),
                         QUOTENAME(Name)),
            @colsWithIsNull=COALESCE(@colsWithIsNull + ',ISNULL('+QUOTENAME(Name)+',''no'') AS '+QUOTENAME(Name),
                         'ISNULL('+QUOTENAME(Name)+',''no'') AS '+QUOTENAME(Name))
    FROM
        CTE2
    WHERE
        iRank=1
    

    Then do a dynamic pivot like this:

    DECLARE @query NVARCHAR(4000)=
    N';WITH CTE
    AS
    (
        SELECT
            #tblContryPassport.userid,
            ''yes'' AS Answer,
            #tblContryPassport.contryPassport AS Name
        FROM
            #tblContryPassport
        UNION ALL
        SELECT
            #tblLanguage.userid,
            ''yes'' AS Answer,
            #tblLanguage.[language] AS Name
        FROM
            #tblLanguage
    )
    SELECT
        userName,
        department,
        '+@colsWithIsNull+'
    FROM
    (
        SELECT
            #tblUser.userid,
            #tblUser.userName,
            #tblUser.department,
            CTE.Answer,
            CTE.Name
        FROM
            #tblUser
            JOIN CTE
                ON #tblUser.userid=CTE.userid
    ) AS p
    PIVOT
    (
        MAX(Answer)
        FOR Name IN ('+@cols+')
    ) AS pvt'
    
    EXECUTE(@query)
    

    Then in my case I will drop the temp tables:

    DROP TABLE #tblUser
    DROP TABLE #tblContryPassport
    DROP TABLE #tblLanguage
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I have a MySQL table with approximately 3000 rows per user. One of the
I have a class User with one field called birthDate which is a java.sql.Date
I have two tables, one that stores user earnings and another that stores the
Hi I have one web service connected to one db that has a table
i have an application in which a user can lock a certain commodity and
I have two tables: Accounts , which contains the information of accounts that a
I'm looking for a way to non-destructively alter data. I have one table that
I have a table view which is many cases will only have one or
If I have a tableview that has say 3 levels that the user 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.