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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:09:12+00:00 2026-06-12T09:09:12+00:00

Let’s suppose I have two tables: Question (Id, Text) Answer (Value, QuestionId, Guid) Column

  • 0

Let’s suppose I have two tables:

Question (Id, Text)
Answer (Value, QuestionId, Guid)

Column Guid groups answers from the same person.

I need a query to produce results like this:

'Question 1' | 'Question 2'
4            | 3
1            | NULL
NULL         | 5
2            | 6
9            | NULL

Questions texts are transformed into column headers and answers values are in rows. Answers are grouped by Guid, so there are answers from one person in one row. If a person didn’t answer particular question, NULL is returned.

Number of questions can vary.

Data used to produce sample results:

 Question
 Id   | Text
 1    | Question 1
 2    | Question 2

 Answer
 Value | QuestionId | Guid
 4     | 1          | AAA
 3     | 2          | AAA
 1     | 1          | BBB
 5     | 2          | CCC
 2     | 1          | DDD
 6     | 2          | DDD
 9     | 1          | EEE

Can you please help me out with a query to produce the results?

  • 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-12T09:09:13+00:00Added an answer on June 12, 2026 at 9:09 am

    Since you have an unknown number of questions then tou will need to use dynamic SQL to PIVOT this:

    DECLARE @colsFinal AS NVARCHAR(MAX),
        @colsPivot AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @colsFinal = STUFF((SELECT distinct ',' 
                          + QUOTENAME(Id) 
                               + ' as Question_'+ cast(Id as varchar(10))
                        from question
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    select @colsPivot = STUFF((SELECT distinct ',' 
                          + QUOTENAME(Id) 
                        from question
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT ' + @colsFinal + ' from 
                 (
                    select questionid, value, guid
                    from question q
                    left join value v
                      on q.id = v.questionid
                ) x
                pivot 
                (
                    min(value)
                    for questionid in (' + @colsPivot + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    If you had a known number of columns, then you could hard-code the values for the PIVOT (See SQL Fiddle With Demo):

    select [1] as Question1, [2] as Question2
    from 
    (
      select questionid, value, guid
      from question q
      left join value v
        on q.id = v.questionid
    ) x
    pivot
    (
      max(value)
      for questionid in ([1], [2])
    ) p
    

    Or you can use an aggregate function with a CASE (See SQL Fiddle With Demo):

    select max(case when q.id = 1 then v.value end) Question1,
      max(case when q.id = 2 then v.value end) Question2
    from question q
    left join value v
      on q.id = v.questionid
    group by guid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say I have a table with a Color column. Color can have various
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let assume we have two activities. A - main activity, that is home launcher
Let's say you have a method that expects a numerical value as an argument.
Let's suppose I have an XML file like this: <?xml version=1.0 encoding=ISO-8859-1?> <MIDIFile> <Event>
Let's say I have two objects, Master and Slave . Slave has a method
Let's say I have the following two lists of tuples myList = [(1, 7),

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.