mysql数据透视表的问题

时间:2013-09-10 22:05:36

标签: mysql sql pivot

我在使用以下表格在MySQL中创建数据透视表时遇到了问题:

Teams
-------------
id | name
1  | teamA

Processes
-------------
id | name
1  | processA
2  | processB

ProcessDetails
---------------
id | processId | keyName
 1 |     1     |  shape
 2 |     1     |  vegetable
 3 |     1     |  fruit
 4 |     2     |  animal

TeamProcesses
-----------------
id | teamId | processId
 5 |   1    |    1
 6 |   1    |    2

TeamProcessDetails
--------------------
id | teamProcessId | proccessDetailsId | value
 1 |       5       |         1         | circle
 2 |       5       |         2         | carrot
 3 |       5       |         3         | apple
 4 |       6       |         4         | dog

我想要制作的数据透视表就是这个:

Pivot Table
------------
teamId | processId |  shape  | vegetable |  fruit  | animal
   1   |     1     |  circle |  carrot   |  apple  |  NULL
   1   |     2     |  NULL   |   NULL    |   NULL  |  dog

应该注意,键的数量是动态的,所以我想我需要使用预处理语句方法。此外,进程没有相同的键,因此它们只应具有属于该进程的键的值。

谢谢!

1 个答案:

答案 0 :(得分:2)

当您尝试调整动态或未知值时,我总是建议您先从查询的静态或硬编码版本开始,然后将其转换为动态SQL。

MySQL没有PIVOT函数,因此您需要使用带有CASE表达式的聚合函数来获取结果。代码的静态版本类似于以下内容:

select t.id teamid, 
  t.name teamname, 
  p.id processid, 
  p.name processname,
  max(case when pd.keyname = 'shape' then tpd.value end) shape,
  max(case when pd.keyname = 'vegetable' then tpd.value end) vegetable,
  max(case when pd.keyname = 'fruit' then tpd.value end) fruit,
  max(case when pd.keyname = 'animal' then tpd.value end) animal
from teams t
inner join teamprocesses tp
  on t.id = tp.teamid
inner join TeamProcessDetails tpd
  on tp.id = tpd.teamProcessId
inner join processes p
  on tp.processid = p.id
inner join processdetails pd
  on p.id = pd.processid
  and tpd.processDetailsid = pd.id
group by t.id, t.name, p.id, p.name;

请参阅SQL Fiddle with Demo

现在,如果您要将未知数量的keynames转换为列,那么您将需要使用prepared statement来生成动态SQL。代码类似于:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(case when pd.keyname = ''',
      keyname,
      ''' then tpd.value end) AS ',
      replace(keyname, ' ', '')
    )
  ) INTO @sql
from ProcessDetails;

SET @sql 
    = CONCAT('SELECT t.id teamid, 
                t.name teamname, 
                p.id processid, 
                p.name processname, ', @sql, ' 
              from teams t
              inner join teamprocesses tp
                on t.id = tp.teamid
              inner join TeamProcessDetails tpd
                on tp.id = tpd.teamProcessId
              inner join processes p
                on tp.processid = p.id
              inner join processdetails pd
                on p.id = pd.processid
                and tpd.processDetailsid = pd.id
              group by t.id, t.name, p.id, p.name;');

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

请参阅SQL Fiddle with Demo

要记住,创建列字符串的GROUP_CONCAT函数有一个默认最大长度为1024,因此如果您要在此字符串中包含大量字符,则可能需要更改group_concat_max_len的会话值。

此查询将给出结果:

| TEAMID | TEAMNAME | PROCESSID | PROCESSNAME |  SHAPE | VEGETABLE |  FRUIT | ANIMAL |
|      1 |    teamA |         1 |    processA | circle |    carrot |  apple | (null) |
|      1 |    teamA |         2 |    processB | (null) |    (null) | (null) |    dog |