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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:28:30+00:00 2026-06-14T05:28:30+00:00

I have a table that looks like the following PIN QuestionNum Response 1111 1

  • 0

I have a table that looks like the following

PIN    QuestionNum     Response
1111   1               1
1111   2               3
2222   1               4
2222   2               3
3333   2               5

The expected output from my query would be:

PIN   Question1   Question2
1111  1           3
2222  4           3
3333  null        5

I don’t know if it is possible to make the output of a query appear like this.

Could anyone please advise if this is possible please.

  • 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-14T05:28:31+00:00Added an answer on June 14, 2026 at 5:28 am

    This is basically a PIVOT but MySQL does not have PIVOT function. So you will want to replicate this using an aggregate function and a CASE statement. If you know the number of QuestionNum values that you have then you can hard-code the query similar to this:

    select pin,
      max(case when QuestionNum = 1 then response end) Question1,
      max(case when QuestionNum = 2 then response end) Question2
    from yourtable
    group by pin
    

    See SQL Fiddle with Demo

    Now, if you have an unknown number of values for QuestionNum, then you can use a prepared statement to generate a dynamic version of this query:

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'max(case when QuestionNum = ''',
          QuestionNum,
          ''' then response end) AS Question',
          QuestionNum
        )
      ) INTO @sql
    FROM yourtable;
    
    SET @sql = CONCAT('SELECT pin, ', @sql, ' 
                      FROM yourtable 
                      GROUP BY pin');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    See SQL Fiddle with Demo

    Both will produce the same results:

    |  PIN | QUESTION1 | QUESTION2 |
    --------------------------------
    | 1111 |         1 |         3 |
    | 2222 |         4 |         3 |
    | 3333 |    (null) |         5 |
    
    • 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 looks like the following: <table class=theClass> <tr> <td class=anotherClass><strong>Label1:</strong></td>
I have a table in MySQL That looks like the following: date |storenum |views
I have a scenario outline table that looks like the following: Scenario Outline: Verify
I have some data in a table that looks roughly like the following: table
Greetings, I have a table that looks like the following: date value 2007-11-05 134
I have data in a table that looks like the following sample data: varchar(20)
I have a table that looks like the following: id, Name, LinkBackId, Param1, Param2,
I have a table that looks like the following where what should be a
I have a table relationship that looks like the following: barn ------ PK barn_id
Suppose I have a table that looks like the following id | location |

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.