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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:50:26+00:00 2026-06-15T14:50:26+00:00

I have two tables, Answers and Users , which look like this: Answers (AnswerID,

  • 0

I have two tables, Answers and Users, which look like this:

Answers (AnswerID, QuestionNumber, Answer, UserID)
Users (UserID, FirstName, LastName)

What SQL query could I run to return a report like this:

enter image description here

  • 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-15T14:50:27+00:00Added an answer on June 15, 2026 at 2:50 pm

    You can use the PIVOT function to transform this data. It seems rather unusual that you have the Name (Firstname/Lastname) as the headers but if that is what you want, then you can use the following:

    select *
    from
    (
      select a.questionnumber,
        u.firstname +' '+ u.lastname as Name,
        a.answer
      from answers a
      left join users u
        on a.userid = u.userid
    ) src
    pivot
    (
      max(answer)
      for name in ([John Smith], [Bob Jones])
    ) piv;
    

    See SQL Fiddle with Demo

    Result:

    | QUESTIONNUMBER | JOHN SMITH | BOB JONES |
    -------------------------------------------
    |              1 |       blah |      test |
    |              2 |      hsdfk |    (null) |
    

    I would think you would want the Question Numbers as the headers with the answers below them like this:

    select *
    from
    (
      select 'QuestionNumber'+cast(a.questionnumber as varchar(10)) questionnumber,
        u.firstname +' '+ u.lastname as Name,
        a.answer
      from answers a
      left join users u
        on a.userid = u.userid
    ) src
    pivot
    (
      max(answer)
      for questionnumber in ([QuestionNumber1], [QuestionNumber2])
    ) piv;
    

    See SQL Fiddle with Demo

    Result:

    |       NAME | QUESTIONNUMBER1 | QUESTIONNUMBER2 |
    --------------------------------------------------
    |  Bob Jones |            test |          (null) |
    | John Smith |            blah |           hsdfk |
    

    The above answers will work great if you have a known number of items to transform. But if you have an unknown number, then you can use dynamic SQL to PIVOT the data:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME('QuestionNumber'+cast(questionnumber as varchar(10))) 
                        from answers
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT Name, ' + @cols + ' from 
                 (
                  select ''QuestionNumber''+cast(a.questionnumber as varchar(10)) questionnumber,
                    u.firstname +'' ''+ u.lastname as Name,
                    a.answer
                  from answers a
                  left join users u
                    on a.userid = u.userid
                ) x
                pivot 
                (
                    max(answer)
                    for questionnumber in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    Result:

    |       NAME | QUESTIONNUMBER1 | QUESTIONNUMBER2 |
    --------------------------------------------------
    |  Bob Jones |            test |          (null) |
    | John Smith |            blah |           hsdfk |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a forum which have some what similar look as this! A user
I have a database for a forum-like website. It has a 'users' table, which
I basically have two tables called images and users . The images table has
I have two types of 'owner', each of which can have one 'preference'. This
Alright, this is a weird problem I'm having. I have two pages, which are
I have these two tables people ============ id, name and answer_sheets ============ id, person_id,
I have two tables images2 and image_data So the goal is to have 1
I have two tables 1.Products prod_id prod_name 1 honda 2 hero 3 marcedes 4
I have two tables, lets say table1 and table2 with common columns, id and
I have two tables: table a ida valuea 1 a 2 b 3 c

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.