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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:29:20+00:00 2026-06-01T13:29:20+00:00

I have a table Survey_Data_Response that is populated with an ‘insert into’ statement from

  • 0

I have a table Survey_Data_Response that is populated with an ‘insert into’ statement from two tables – Survey_Question and Survey_Response, they are joined on the QuestionID.

I want to use a SP to pivot the Survey_Data_Response table and save the results to a temp table so I can query it to develop reports.

The Survey_Data_Response table has fields – QuestionID, ResponseID, Question, Response, ResponseDateTime,moduleID.

The number of questions can change depending on the survey.

 QuestionID   Question   Response    ResponseDateTime       ResponseID   ModuleID
 123           Age          34       2011-06-06 18:21:00     ABC            123
 345           Gender       M        2011-06-06 18:21:00     DEF            123
 567           Phone       444-4444  2011-06-06 18:21:00     HIG            123
 123           Age          23       2011-06-07 12:01:00     MNO            123
 789           Postal Code 90988     2011-06-07 12:01:00     XYZ            123

I need to pivot the table to look like this, the questions as columns and the response in the appropriate field.

   ResponseID   Age    Gender     Phone     Postal Code     ResponsDateTime 
   ABC          34      M        444-4444                   2011-06-06 18:21:00
   XYZ          23                            90988         2011-06-07 12:01:00

I’ve tried numerous pivot queries over the last 2 days but haven’t had any luck. This is where I’m at, it returns the column headers, but the responses to the questions aren’t being populated.

 DECLARE @cols VARCHAR(1000)
 DECLARE @sqlquery VARCHAR(2000)

 SELECT  @cols = STUFF(( SELECT distinct  ',' + QuoteName(question)
                    FROM temp_SURVEY_DATA FOR XML PATH('') ), 1, 1, '') 
 SET @sqlquery = 'SELECT * FROM
  (SELECT DynamicQuestionResponseID,question,moduleid 
   FROM temp_SURVEY_DATA ) base
   PIVOT (max(moduleid) FOR question
   IN (' + @cols + ')) AS finalpivot'

 EXECUTE ( @sqlquery )

Can someone help me understand why this Pivot doesn’t work and how to make it work?

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-06-01T13:29:22+00:00Added an answer on June 1, 2026 at 1:29 pm

    this should give you what you want:

    create table temp_SURVEY_DATA
    (
        questionid int,
        question varchar(50),
        response varchar(50),
        responsedatetime datetime,
        responseid varchar(3),
        moduleid int
    )
    
    insert into temp_SURVEY_DATA values(123, 'Age', '34', '2011-06-06 18:21:00', 'ABC', 123)
    insert into temp_SURVEY_DATA values(345, 'Gender', 'M', '2011-06-06 18:21:00', 'ABC', 123)
    insert into temp_SURVEY_DATA values(567, 'Phone', '444-4444', '2011-06-06 18:21:00', 'ABC', 123)
    insert into temp_SURVEY_DATA values(123, 'Age', '23', '2011-06-07 12:01:00', 'XYZ', 123)
    insert into temp_SURVEY_DATA values(789, 'Postal Code', '90988', '2011-06-07 12:01:00', 'XYZ', 123)
    
    DECLARE @cols AS VARCHAR(MAX),
        @query  AS VARCHAR(MAX);
    
    SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.question) 
                FROM dbo.temp_SURVEY_DATA c
                FOR XML PATH(''), TYPE
                ).value('.', 'VARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT responseid, moduleid, responsedatetime, ' + @cols + ' from 
                (
                    select question, response, responsedatetime, responseid, moduleid
                    from temp_SURVEY_DATA
                ) x
                pivot 
                (
                    max(response)
                    for question IN(' + @cols + ')
                ) p '
    
    execute(@query)
    

    Results:

    responseid    moduleid  responsedatetime        Age Gender  Phone       Postal Code
    ABC           123       2011-06-06 18:21:00.000 34  M       444-4444     
    XYZ           123       2011-06-07 12:01:00.000 23                      90988
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that consists of two columns: question and answer. I currently
I have table rows of data in html being filled from a CGI application.
I an trying to INSERT multiple rows into an SQL, Oracle table though SQL
I have an application that receives files with a flat table in DBF, which
I have a table for a survey, and wants to delete all records that
I have a working survey that pulls each question and any related answers from
I have a table of data with survey results, and I want to do
Suppose I have an Oracle 11.2 database containing the following table: TABLE: SURVEY PARAMETER
I have a table of surveys which contains (amongst others) the following columns survey_id
I have table definition like below: Place (id, name) Review (id, userid, placeid) Favorite

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.