2

我无法使用以下表格在 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

需要注意的是,键的数量是动态的,所以我认为我需要使用准备好的语句方法。此外,这些进程没有相同的键,因此它们应该只具有属于该进程的键的值。

谢谢!

4

1 回答 1

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转换为列,那么您将需要使用准备好的语句来生成动态 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 |
于 2013-09-11T01:25:40.110 回答