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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:42:34+00:00 2026-06-15T20:42:34+00:00

I have a SQL Server view with following data: ID clientID surveyID questionID q_optionID

  • 0

I have a SQL Server view with following data:

ID   clientID  surveyID    questionID     q_optionID   q_ans_text
-----------------------------------------------------------------
1       1          1           1            NULL            Yes
2       1          1           2             18             NULL 
3       1          1           3             19             NULL
4       2          1           1            NULL             No
5       2          1           2             18             NULL
6       2          1           3             19             NULL
7       3          2           1            NULL            Yes 
8       3          2           2             15             NULL 
9       3          2           3             13             NULL   

I want the result to be something like this:

ClientID  SurveyID   Q1    Q2    Q3  
------------------------------------
   1          1     Yes    18    19   
   2          1      No    18    19   
   3          2     Yes    15    13 

Where the conditional NULL values are ignored and the proper answer is placed in the column. I have looked at Pivot table examples, but they seem to be focusing on single column pivots.

  • 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-15T20:42:36+00:00Added an answer on June 15, 2026 at 8:42 pm

    In order to perform this transformation you will need to UNPIVOT and then PIVOT the data. The UNPIVOT will take the values from your q_optionID and q_ans_text columns and transform it into two columns one with the value and the column name.

    There are two ways that you can PIVOT this, you can hard-code all of the values using a static version or you can use dynamic sql. In order to UNPIVOT the data you need to be sure that the data is of the same datatype, so converting might be necessary.

    Static PIVOT:

    select clientid, surveyid, 
        questionid,
        value,
        col
      from
      (
        select clientid, surveyid, questionid,
          cast(q_optionID as varchar(4)) q_optionID,
          q_ans_text
        from yourtable
      ) s
      unpivot
      (
        value
        for col in (q_optionID, q_ans_text)
      ) un
    

    See SQL Fiddle with Demo

    Unpivot result:

    | CLIENTID | SURVEYID | QUESTIONID | VALUE |        COL |
    ---------------------------------------------------------
    |        1 |        1 |          1 |   Yes | q_ans_text |
    |        1 |        1 |          2 |    18 | q_optionID |
    |        1 |        1 |          3 |    19 | q_optionID |
    |        2 |        1 |          1 |    No | q_ans_text |
    |        2 |        1 |          2 |    18 | q_optionID |
    |        2 |        1 |          3 |    19 | q_optionID |
    |        3 |        2 |          1 |   Yes | q_ans_text |
    |        3 |        2 |          2 |    15 | q_optionID |
    |        3 |        2 |          3 |    13 | q_optionID |
    

    Then you will apply the PIVOT to the result to get your final product.

    select *
    from
    (
      select clientid, surveyid, 
        'Q'+cast(questionid as varchar(10)) question,
        value
      from
      (
        select clientid, surveyid, questionid,
          cast(q_optionID as varchar(4)) q_optionID,
          q_ans_text
        from yourtable
      ) s
      unpivot
      (
        value
        for col in (q_optionID, q_ans_text)
      ) un
    ) src
    pivot
    (
      max(value)
      for question in (Q1, Q2, Q3)
    ) piv
    

    See SQL Fiddle with demo

    Dynamic PIVOT:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME('Q'+cast(questionid as varchar(10))) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT clientid, surveyid,' + @cols + ' from 
                 (
                    select clientid, surveyid, 
                      ''Q''+cast(questionid as varchar(10)) question,
                      value
                    from
                    (
                      select clientid, surveyid, questionid,
                        cast(q_optionID as varchar(4)) q_optionID,
                        q_ans_text
                      from yourtable
                    ) s
                    unpivot
                    (
                      value
                      for col in (q_optionID, q_ans_text)
                    ) un
                ) x
                pivot 
                (
                    max(value)
                    for question in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    UNION ALL/Aggregate with Case Version:

    Now, if you are working in a system that does not have PIVOT then you can use a UNION ALL to UNPIVOT and an aggregate function with a CASE to PIVOT:

    select clientid, surveyid,
      max(case when questionid = 1 then value end) Q1,
      max(case when questionid = 2 then value end) Q2,
      max(case when questionid = 3 then value end) Q3
    from
    (
      select clientid, surveyid, questionid, cast(q_optionID as varchar(10)) value, 'q_optionID' col
      from yourtable
      union all
      select clientid, surveyid, questionid, q_ans_text value, 'q_ans_text' col
      from yourtable
    ) unpiv
    group by clientid, surveyid
    

    See SQL Fiddle with Demo

    All three will produce the same result:

    | CLIENTID | SURVEYID |  Q1 | Q2 | Q3 |
    ---------------------------------------
    |        1 |        1 | Yes | 18 | 19 |
    |        2 |        1 |  No | 18 | 19 |
    |        3 |        2 | Yes | 15 | 13 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view in SQL Server 2008 that I want to use for
I have a Sql Server 2005 table with following data: idHearing is the primary
Imagine I have the following SELECT statement in a view (SQL Server 2008): SELECT
I have an SQL Server Reporting Services (SSRS) report, that uses a view. That
I currently have this statement in my SQL View (SQL Server 2008 R2) which
In SQL Server I have put a clustered index on a view to eliminate
I have tried the following query in Microsoft SQL Server Management Studio: select ROW_NUMBER()
I have the following situation (SQL Server Express): 2 tables connected via a pk-fk
I've got data in SQL Server 2000 and have a HyperLink that goes to
I have the following 2 tables defined in a SQL Server database: CREATE TABLE

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.