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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:16:04+00:00 2026-06-13T12:16:04+00:00

I am trying to write a sql statement that creates a pivot table. The

  • 0

I am trying to write a sql statement that creates a pivot table.

The data is a list of survey responses and is made from a join query involving tables respondent, response and answer. The answer table is structured as below:

______________________________________________________________
|AnswerID|ResponseID|QuestionID|AnswerComment                |
|      1 |        1 |        1 |This is answer 1             |
|      2 |        1 |        2 |This is answer 2             |
|      3 |        1 |        3 |This is answer 3             |
|      4 |        2 |        1 |Answer 1 Respondent 2        |
|      5 |        2 |        3 |Resp 2, No Q.2               |
|      6 |        3 |        2 |I answered Q.2 only          |
|____________________________________________________________|

Respondent looks like:

______________________________________________________________
|RespondentID|GivenName|Surname|age, gender, blah blah       |
|____________________________________________________________|

Response:

______________________________________________________________
|ResponseID|ResponseDate|RespondentID|Attachment             |
|____________________________________________________________|

and Question:

_____________________________________________________
|QuestionID|QuestionNumber|QuestionText|            |
|___________________________________________________|

I want my output in the form of:

________________________________________________________________________________________
|RespondentInfo|ResponseDate|1                     |2                  |3               |
|Respondent 1  |15/10/2012  |This is answer 1      |This is answer 2   |This is answer 3|
|Respondent 2  |17/10/2012  |Answer 1 Respondent 2 |                   |Resp 2, No Q.2  |
|Respondent 3  |19/10/2012  |                      |I answered Q.2 only|                |
|______________________________________________________________________________________|

I want to get one line per respondent with each answer in the corresponding column. The query I have created – although it pivots the answers to the correct columns – provides a new row for each answer with nulls in all the blank columns.

My sql:

SELECT Respondent, ResponseDate, 
         [1], [2], [3]
FROM  (
       SELECT  Respondent.Surname + ', ' + FirstName as RespondentInfo, 
               Response.ResponseDate, 
               isnull(Answer.AnswerComment, '') as Answer, 
               Qustion.QuestionNumber
       FROM    Answer INNER JOIN
                Question ON Answer.QuestionID = Question.QuestionID INNER JOIN
                   Response ON Answer.ResponseID = Response.ResponseID INNER JOIN
                   Respondent ON Response.RespondentID = Respondent.RespondentID
       ) as ResponseQuery
PIVOT (
    max(Answer)
FOR ResponseQuery.QuestionNumber in ([1], [2], [3])
       ) AS OutputTable

I think the issue is because I am not using an aggregate function so have defaulted to max(). Can anyone point me in the right direction.

Apologies if my question is too verbose.

  • 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-13T12:16:05+00:00Added an answer on June 13, 2026 at 12:16 pm

    The problem is the source, not the pivot.

    It’s hard to tell without seeing the other tables, but using your example table

    select ResponseID, [1],[2],[3] from 
        (select ResponseID, AnswerComment, QuestionID from yourtable) s
    PIVOT (max(AnswerComment) FOR QuestionID in ([1], [2], [3]) ) AS OutputTable
    

    produces the output in the form you’re after.

    If you can’t figure out how to adjust the source to provide the data you want, you can join after the pivot by adding something like…

     inner join Response on OutputTable.ResponseID = Response.ResponseID 
    

    after the above pivot to give

    select Response.ResponseDate, [1],[2],[3] from 
        (select ResponseID, AnswerComment, QuestionID from yourtable) s
    PIVOT (max(AnswerComment) FOR QuestionID in ([1], [2], [3]) ) AS OutputTable
    inner join Response on OutputTable.ResponseID = Response.ResponseID 
    

    etc…

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

Sidebar

Related Questions

I'm trying to write an insert statement for a SQL Server table that inserts
I'm trying to write sql that produces the desired result from the data below.
I'm trying to write an SQL Statement that needs to loop through a list
I am trying to write a SQL statement that will create a flattened table
I'm trying to write a sql statement that will insert data given a few
I am trying to find out how to write an SQL statement that will
I'm trying to write a SQL script that inserts 2 rows into a table
I'm trying to write an SQL statement that duplicates all rows WHERE employee =
I have a perplexing SQL select statement (ugly) that I'm trying to write in
I am trying to write a sql statement that I have 2 tables Store

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.