0

I have a table which looks as following:

    + ------- + -----------+ ------------ + --------- + ---------- +
    | Info_Id | category   | EAN          | Info_Type | Info_Value |
    + ------- + -----------+ ------------ + --------- + ---------- +
    | 1       | 1          | 0123456789   | brand     | brand1     |
    | 2       | 1          | 0123456789   | type      | type1      |
    | 3       | 1          | 0123456789   | price     | 0.00       |
    | 4       | 2          | 9876543210   | brand     | brand6     |
    | 5       | 2          | 9876543210   | type      | type3      |
    | 6       | 2          | 9876543210   | price     | 15.00      |
    | 7       | 2          | 6548214656   | brand     | brand34    |
    | 8       | 2          | 6548214656   | type      | type1      |
    | 9       | 2          | 6548214656   | price     | 99.00      |
    | 10      | 3          | 245511411241 | brand     | brand324   |
    | 11      | 3          | 245511411241 | type      | type1      |
    | 12      | 3          | 245511411241 | price     | 98.00      |
    + ------- + -----------+ ------------ + --------- + ---------- +

Now, I am looking for an query that creates the following output:

    + ------------ + ---------- + --------- + ---------- + ----------+
    | EAN          | category   | brand     | type       | price     |
    + ------------ + ---------- + --------- + ---------- + ----------+
    | 0123456789   | 1          | brand1    | type1      | 0.00      |
    | 9876543210   | 2          | brand6    | type3      | 15.00     |
    | 6548214656   | 2          | brand34   | type1      | 99.00     |
    | etc.         | etc.       | etc.      | etc.       | etc.      |
    + ------------ + ---------- + --------- + ---------- + --------- +

I have the following, but that does not seems to work:

    SELECT ean,
           GROUP_CONCAT(Info_Type)    AS Info_Type,
           GROUP_CONCAT(Info_Value) AS Info_Value
    FROM tablename WHERE category=2
    GROUP BY EAN

However, this provides me something like:

    + ------------ + ---------------- + ------------------- + 
    | EAN          | Info_Type        | Info_Value          | 
    + ------------ + ---------------- + ------------------- +
    | 0123456789   | brand,type,price | brand1,type1,0.00   |
    | 9876543210   | brand,type,price | brand6,type3,15.00  |
    | 6548214656   | brand,type,price | brand34,type1,99.00 |
    | etc.         | etc.             | etc.                |
    + ------------ + ---------------- + ------------------- +

How do I do this the right way?

2 Answers 2

1

I think you just want conditional aggregation:

select ean, category,
       max(case when info_type = 'brand' then info_value end) as brand,
       max(case when info_type = 'type' then info_value end) as type,
       max(case when info_type = 'price' then info_value end) as price
from t
where category = 2
group by ean, category;
Sign up to request clarification or add additional context in comments.

6 Comments

why you group by category if you only have category 2 in the where
@BerndBuffen . . . (1) I always group by all unaggregated columns; (2) The OP's output has categories other than "2", so I'm not sure if s/he intends the where clause.
This seems to work, but is a bit slow. Table has indexes, how can I improve this query even more? (1M rows)
@mh3982 . . . An index on t(category, ean, info_type, info_value) might help.
@GordonLinoff an index at all of them together? Or individual indexes for each column?
|
0

You must modify each GROUP_CONCAT like this:

SELECT ean,
  GROUP_CONCAT(IF(Info_Type = 'type' ,Info_Type,''))    AS 'type',
  GROUP_CONCAT(IF(Info_Type = 'price' ,Info_Type,''))    AS 'price'
FROM tablename WHERE category=2
GROUP BY EAN;

Comments

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.