1

I'm having trouble creating a pivot table in MySQL using the following tables:

Teams
-------------
id | name
1  | teamA
2  | teamB

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

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

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

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

The pivot table I am trying to produce should only contain the process details for a given team.

Example

For Team A:

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

For Team B:

teamId | processId | dessert
   2   |     3     | chocolate

Thanks!

2
  • How dynamic should it be? Do you know at design time how many columns there will be and their titles? Also, please post your own best attempt to solve the problem. Commented Sep 27, 2013 at 19:31
  • Sorry I should have mentioned this was a followup question from another I posted a while ago. @bluefeet had helped me out before. stackoverflow.com/questions/18729532/… Commented Sep 27, 2013 at 20:18

1 Answer 1

2

Using the query from your previous question you should be able to alter the code to add a WHERE clause that will filter the data for each team:

SET @sql = NULL;
set @team = 'teamA';

SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(case when pd.keyname = ''',
      keyname,
      ''' then tpd.value end) AS ',
      replace(keyname, ' ', '')
    )
  ) INTO @sql
from ProcessDetails
where processId in (select tp.processId
                    from teams t
                    inner join teamprocesses tp
                      on t.id = tp.teamid
                    where t.name = @team);

SET @sql 
    = CONCAT('SELECT t.id teamid, 
                t.name teamname, 
                p.id processid, ', @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
              where t.name = ''', @team, ''' 
              group by t.id, t.name, p.id, p.name;');

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

See SQL Fiddle with Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help again. I couldn't get it to work because of the subquery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.