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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:56:14+00:00 2026-05-18T19:56:14+00:00

There are lots of posts out there on pivoting rows into columns for various

  • 0

There are lots of posts out there on pivoting rows into columns for various databases. They seem to fall into two camps, using case statements or using a built in function of the database vendor. I am using MySQL and have not found anything so far on any built in function that will allow me to pivot on an arbitrary unknown number of row values that I want to pivot into columns. If I don’t know the values ahead of time, I can’t build the CASE queries that appear frequently on stackoverflow. I want to know if there is something in MySQL similar to these in other databases where it is called crosstab or pivot:

-Postgresql: http://www.postgresql.org/docs/current/static/tablefunc.html
-Oracle: http://www.oracle-base.com/articles/11g/PivotAndUnpivotOperators_11gR1.php
-SQL Server: http://msdn.microsoft.com/en-us/library/ms177410.aspx

Just to ensure that I’m clear what I’m asking for when I say pivot rows to columns, I want to transform a table like this

user_id      key       value
bob            hair        brown
bob           
eyes        blue
jake            hair        brown
jake           
height        6’2″

Into this:

user_id      hair       eyes       height
bob            brown
     blue
       
jake            brown
        
           6’2″

I am looking specifically for a solution in MySQL, so if there is anything database specific that is new or coming out that you know about that can solve this it would be greatly appreciated.

  • 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-05-18T19:56:15+00:00Added an answer on May 18, 2026 at 7:56 pm

    The link that Kangkan provided will show you how to pull this off had you known the column names in advance. We’re going for the same logic, except using dynamic SQL to build the statement. There is 2 parts to each field that you need to include, the field in the select statement and an appropriate join to get the value…so we’ll need to build the statement in two parts

    First declare 3 variables to build this…I’ll go with @select, @join, and @sql for this example. Give the variables the initial values

     set @select = 'select user_id,'
     set @join = 'from table t'
    

    now declare and load a cursor with the distinct values in the table.key field. I’m going to use @field as the variable gets populated with the distinct table.key field. Then loop through it building the two variables:

     set @select = @select + ', ' + @field + '.value as '+@field+'
     set @join = @join + ' left join table ' + @field + 'on '+@field+'.key = t.key and and '+@field+'.user_id = t.user_id
    

    (the join is designed to use the value in @field as the alias of the table)

    loop through your cursor building out @select and @join. At the end of the loop:

    set @sql = @select + @join + 'where clause if you want'
    exec @sql
    

    Dynamic SQL built like this can be an absolute pain to trouble shoot (and get right) and open up security issues…but it’s about the only way I can see this accomplished. Watch for size restrictions on your variables….if you have too many distinct Key’s there, the variables grow too big. Sorry I can’t be more exact with the pseudo on this…you’ll find building dynamic sql in sql is painstaking.

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

Sidebar

Related Questions

There are lots of posts out there on pivoting rows into columns for various
There are lots of posts on here about moving a folder out of one
There's lots of good material on Design Patterns out there that covers how you
I don't think I need to post my code (there's lots of it) but
There are lots of PHP articles about the subject so is this a PHP
There are lots of web application frameworks available these days, for pretty much every
There are lots of questions on how to improve communication between teams. One way
There are lots of non-image-based CAPTCHA ideas floating around. But what about the old-fashioned
There are lots of tools for creating installers on Windows (InstallShield, InnoSetup, NSIS, just
There's lots of information about how cached execution plans in SQL Server eliminate much

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.