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

  • Home
  • SEARCH
  • 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 9151361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:46:59+00:00 2026-06-17T11:46:59+00:00

I’ve been looking through StackOverflow questions and haven’t really found one for this issue.

  • 0

I’ve been looking through StackOverflow questions and haven’t really found one for this issue.

I have a table that’s arranged basically like:

Table.LineID
Table.QuestionGroup
Table.Question
Table.Answer

And I’d like to “pivot” the table, but the questions and answers don’t have anything to aggregate. They’re all just text fields for example, one might read:

Table.LineID = 00001
Table.QuestionGroup = Color
Table.Question = Outside Color
Table.Answer = Dark Green

Table.LineID = 00001
Table.QuestionGroup = Size
Table.Question = Height
Table.Answer = 180 3/4

I’m trying to pivot the table so that I can get the associated ID of line and make that the spreading function the Questions So it would look like

LineID | Question 1 | Question 2 | Etc…

| Answer 1 | Answer 2 | Etc…

I’ve looked at this https://stackoverflow.com/a/7744347/839330 except this seems to be more in reference to some VB code (maybe I’m wrong?). Does anyone have an idea on how I should approach this?

  • 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-17T11:47:00+00:00Added an answer on June 17, 2026 at 11:47 am

    Just because you have text data in your table does not mean that you cannot use an aggregate function on it.

    You did not specify what RDBMS you are using but this type of data transformation is a pivot. This converts row data into columns. Some databases has a pivot function but if you are using one without that function, then you will need to use an aggregate function with a CASE expression:

    select lineid,
      max(case when question ='Height' then answer else '' end) Height,
      max(case when question ='Outside Color' then answer else '' end) [Outside Color]
    from yourtable 
    group by lineid
    

    See SQL Fiddle with Demo

    If you have a database that has the pivot function (SQL Server 2005+/Oracle 11g+), then your code will be similar to this:

    select *
    from 
    (
      select lineid,
        Question,
        Answer
      from yourtable
    ) src
    pivot
    (
      max(answer)
      for question in ([Height], [Outside Color])
    ) piv;
    

    See SQL Fiddle with Demo

    Now if you are using SQL Server 2005+ and you have an unknown number of questions that you want to turn into columns, then you can use dynamic sql similar to this:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Question) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT lineid,' + @cols + ' from 
                 (
                    select lineid,
                      Question,
                      Answer
                    from yourtable
                ) x
                pivot 
                (
                    max(Answer)
                    for Question in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with demo

    Based on your sample data the result is:

    | LINEID |  HEIGHT | OUTSIDE COLOR |
    ------------------------------------
    |      1 | 180 3/4 |    Dark Green |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.