0

Possible Duplicate:
Select MYSQL rows but rows into columns and column into rows

How to display from:

╔══════╦════╦════╗
║ Code ║ A  ║ B  ║
╠══════╬════╬════╣
║ xxxx ║ 50 ║ 63 ║
║ yyyy ║ 38 ║ 68 ║
║ .... ║ .. ║ .. ║
╚══════╩════╩════╝

to like this:

╔══════╦══════╦══════╦═════╗
║ Code ║ xxxx ║ yyyy ║ ... ║
╠══════╬══════╬══════╬═════╣
║ A    ║   50 ║   63 ║ ... ║
║ B    ║   38 ║   68 ║ ... ║
╚══════╩══════╩══════╩═════╝

As it may have a few "code" like xxxx, yyyy, zzzz, ...
So, I need to display all the "code" in dynamically without hard-coded.

5
  • There is no standard way to achieve this dynamically just using regular sql Commented Oct 23, 2012 at 7:15
  • btw, i need to get the header without hardcoded. is that possible? Commented Oct 23, 2012 at 7:24
  • Have a look at these links - Dynamic pivot tables (transform rows to columns), Automate pivot table queries. Commented Oct 23, 2012 at 8:25
  • What happens if there are multiple records with the same Code? Commented Oct 23, 2012 at 9:44
  • Hope there is an index to group data. Commented Oct 23, 2012 at 10:53

1 Answer 1

1

Unfortunately, MySQL does not have an UNPIVOT or a PIVOT function but they can be replicated using a UNION ALL for UNPIVOT and an aggregate function with CASE statement for the PIVOT. If you know the values that you want to transform, then you can hard-code then similar to the following:

select col code,
  sum(case when code = 'xxxx' then value end) xxxx,
  sum(case when code = 'xxxx' then value end) yyyy
from
(
  select code, A value, 'A' col
  from table1
  union all
  select code, B value, 'B' col
  from table1
) x
group by col;

See SQL Fiddle with Demo

But you stated that you will have an unknown number of code values. If that is the case then you can use a prepared statement to pivot the data:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'SUM(case when code = ''',
      code,
      ''' then value end) AS ',
      code
    )
  ) INTO @sql
FROM Table1;

SET @sql = CONCAT('SELECT col as code, ', @sql, ' 
                  FROM
                  (
                    select code, A value, ''A'' col
                    from table1
                    union all
                    select code, B value, ''B'' col
                    from table1
                  ) x
                  GROUP BY col');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

See SQL Fiddle with Demo.

Both produce the same results:

| CODE | XXXX | YYYY |
----------------------
|    A |   50 |   38 |
|    B |   63 |   68 |
Sign up to request clarification or add additional context in comments.

2 Comments

how about if this question in "toad for oracle"(plsql). what is the solution for it? Thanks a lot to anyone help.
@Jimmy428 what version of Oracle? The answer is different depending on the version.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.