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.

Community
  • 1
  • 1
Jimmy428
  • 31
  • 1
  • 1
  • 7
  • There is no standard way to achieve this dynamically just using regular sql – Ertunç Oct 23 '12 at 07:15
  • btw, i need to get the header without hardcoded. is that possible? – Jimmy428 Oct 23 '12 at 07:24
  • Have a look at these links - [Dynamic pivot tables (transform rows to columns)](http://buysql.com/mysql/14-how-to-automate-pivot-tables.html), [Automate pivot table queries](http://www.artfulsoftware.com/infotree/queries.php#523). – Devart Oct 23 '12 at 08:25
  • What happens if there are multiple records with the same `Code`? – eggyal Oct 23 '12 at 09:44
  • Hope there is an index to group data. – Devart Oct 23 '12 at 10:53

1 Answers1

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 |
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • how about if this question in "toad for oracle"(plsql). what is the solution for it? Thanks a lot to anyone help. – Jimmy428 Oct 24 '12 at 01:11
  • @Jimmy428 what version of Oracle? The answer is different depending on the version. – Taryn Oct 24 '12 at 10:18