5

I have a table of product parts like this:

Parts

part_id      part_type      product_id
--------------------------------------
1            A              1
2            B              1
3            A              2
4            B              2
5            A              3
6            B              3

and I want a query that will return a table like this:

product_id      part_A_id      part_B_id
----------------------------------------
1               1              2
2               3              4
3               5              6

In its actual implementation there will be millions of product parts

Taryn
  • 242,637
  • 56
  • 362
  • 405
Vaughan
  • 162
  • 3
  • 9
  • 5
    [What have you tried?](http://whathaveyoutried.com/) – ruakh Sep 27 '12 at 21:15
  • 3
    What you're looking for is usually referred to as a dynamic pivot. – Joe Stefanelli Sep 27 '12 at 21:15
  • How many part types are there? – Lamak Sep 27 '12 at 21:17
  • @ruakh nothing pretty or useful, I've forgotten so much mysql – Vaughan Sep 27 '12 at 21:19
  • @Lamak There will be about 20 product parts – Vaughan Sep 27 '12 at 21:20
  • @JoeStefanelli Will google dynamic pivots now – Vaughan Sep 27 '12 at 21:20
  • SQL Server or MySQL or other? – PeteGO Sep 27 '12 at 21:23
  • 1
    Re: "nothing pretty or useful": Part of the point of showing what you've tried is simply to show that you've tried *something*. (A lot of question-askers don't, and it can be hard to summon the motivation to help those folks!) Another part is to help clarify the question; if you post a query that you've already written, and explain why it doesn't do what you need -- even if it doesn't come close -- then that can help clarify what it is that you do need. – ruakh Sep 27 '12 at 21:28
  • @ruakh I hear what you are saying. In this case I thought my attempt would do little but muddy the waters. – Vaughan Sep 27 '12 at 21:50
  • possible duplicate of [MySQL pivot row into dynamic number of columns](http://stackoverflow.com/questions/12004603/mysql-pivot-row-into-dynamic-number-of-columns) – RichardTheKiwi May 03 '13 at 08:54
  • possible duplicate of [MySQL pivot table](http://stackoverflow.com/questions/7674786/mysql-pivot-table) –  Aug 15 '14 at 07:39

3 Answers3

13

Unfortunately, MySQL does not have a PIVOT function but you can model it using an aggregate function and a CASE statement. For a dynamic version, you will need to use prepared statements:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(case when part_type = ''',
      part_type,
      ''' then part_id end) AS part_',
      part_type, '_id'
    )
  ) INTO @sql
FROM
  parts;
SET @sql = CONCAT('SELECT product_id, ', @sql, ' 
                  FROM parts 
                   GROUP BY product_id');

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

See SQL Fiddle With Demo

If you had only a few columns, then you can use a Static version:

select product_id,
  max(case when part_type ='A' then part_id end) as Part_A_Id,
  max(case when part_type ='B' then part_id end) as Part_B_Id
from parts
group by product_id
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • Why is max here ? Is it because it needs to be aggreate function there and it also could be sum or something else ? – Harlsten Jan 29 '16 at 18:23
  • @Harlsten There has to be an aggregate function for this. `sum` wouldn't make sense because it's a `part_id`, for this particular example you'd need to use `max` or `min`. – Taryn Jan 29 '16 at 18:28
1

SQL Server has a PIVOT keyword, but with MySQL you will need to use either a lot of CASE/IF statements or a lot of JOINs.

Here is a previous post of how to do this.

Community
  • 1
  • 1
PeteGO
  • 5,597
  • 3
  • 39
  • 70
-3

just reference it twice - (you don't really have much of a question here)

select a.product_id, a.part_id "part_a_id", b.part_id "part_b_id"
from parts a, parts b
where a.product_id = b.product_id
Randy
  • 16,480
  • 1
  • 37
  • 55
  • I think you misread the question. You should go back and re-examine his example. – ajon Sep 27 '12 at 21:55
  • i think this produces the results requested. the question is poor imo. – Randy Sep 28 '12 at 01:16
  • Unless I am mistaken this produces additional results that are not in his results. For example: it would produce a row with (1,1,1) as well as (1,1,2) as well as (1,2,1) as well as (1,2,2). – ajon Sep 28 '12 at 05:44