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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:33:24+00:00 2026-06-09T20:33:24+00:00

I have questionnaire data in, SQL Server 2008, that I want to transpose to

  • 0

I have questionnaire data in, SQL Server 2008, that I want to transpose to a matrix.
I saw several posts about the same topic, but I just don’t get pivoting.

Given are following tables:

Question table

Answer table

Customer table

The columns:
[CustomerID], [QuestionName_1], .., [QuestionName_n] <- dynamic number of question columns)
The data:
CustomerID, Answer_1, .., Answer_n

The code to retrieve the columns:

DECLARE @columns VARCHAR(8000)

SELECT @columns = COALESCE(@columns + ',[' + cast(QuestionName as varchar) + ']',
'[' + cast(QuestionName as varchar)+ ']')
FROM Answer A 
INNER JOIN Question Q ON A.QuestionID = Q.QuestionID
INNER JOIN Customer C ON A.CustomerID = C.CustomerID
GROUP BY Q.QuestionName

SET @columns = '[CustomerID],' + @columns

DECLARE @query VARCHAR(8000)
SET @query = 'Some PIVOT query without aggregation'

EXECUTE(@query)

The initial query idea was taken from pivots with dynamic columns.

Can it be done and what would the pivoting query look like?
ps: I don’t want to use ranking with a maximum number of columns.

Regards,

Michel

  • 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-09T20:33:26+00:00Added an answer on June 9, 2026 at 8:33 pm

    Yes you can perform a dynamic pivot. Sometimes it is easier to work up the PIVOT query using a static version first so you can see how the query and results will appear. Then transform the query into a dynamic version.

    Here is an example of a static vs. dynamic version of a query:

    Static (SQL Fiddle):

    select *
    from 
    (
        select u.userid,
            u.fname,
            u.lname,
            u.mobile,
            r.question,
            r.choice
        from users u
        left join results r
            on u.questionid = r.questionid
            and u.choiceid = r.choiceid
    ) x
    pivot
    (
        min(choice)
        for question in([are you], [from])
    ) p
    

    Dynamic (SQL Fiddle):

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.question) 
                FROM results c
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT userid, fname, lname, mobile, ' + @cols + ' from 
                (
                    select u.userid,
                        u.fname,
                        u.lname,
                        u.mobile,
                        r.question,
                        r.choice
                    from users u
                    left join results r
                        on u.questionid = r.questionid
                        and u.choiceid = r.choiceid
               ) x
                pivot 
                (
                    min(choice)
                    for question in (' + @cols + ')
                ) p '
    
    
    execute(@query)
    

    If you can provide more details around your current table structure and then some sample data. We should be able to help you create the version that you would need for your situation.

    As I said though, sometimes it is easier to start with a static version, where you hard-code in the columns that you need to transform first, then move on to the dynamic version.

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

Sidebar

Related Questions

I have an ASP.Net questionnaire application that resubmits data to the same page, showing
I have documents in Mongo that contain questionnaire-like data (company's name, address, phone numbers,
Using SQL Server 2K8 R2, I have two related tables - Member and Questionnaire.
I have an MVC questionnaire app that loops through a set of questions <%
We have a desktop app that allows users to fill in a questionnaire. For
I have a questionnaire viewcontroller class. This instantiates several questionviewcontrollers (each questionviewcontroller has an
I have a custom UITableViewCell that has a UISegmentedControl object. My UITableView (questionnaire form)
I have successfully completed a multiple imputation on the missing data of my questionnaire
I have a timestamp entry of 4 students answering an online questionnaire in a
I'm trying to build a questionnaire / personality test with jQuery. I have an

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.