views:

51

answers:

3

I have this table

------------------
1  | 20,00 | A  |
2  | 20,00 | A  |
3  | 20,00 | A  |
4  | 20,00 | A  |
1  | 50,00 | B  |
2  | 50,00 | B  |
3  | 50,00 | B  |
4  | 50,00 | B  |

I wold like to produce this one using group by.

id   | A     | B     |
----------------------
1    | 20,00 | 50,00 |
2    | 20,00 | 50,00 |
3    | 20,00 | 50,00 |
4    | 20,00 | 50,00 |

Can you help me?

+3  A: 

This doesn't look like a group by problem. But you can easily solve it with a join.

SELECT
  a.id,
  a.a,
  b.b
FROM table AS a
JOIN table AS b ON a.id = b.id
WHERE a.name = 'A' AND b.name = 'B'
WoLpH
+5  A: 

It's a standard pivot query:

  SELECT t.id,
         MAX(CASE WHEN t.col = 'A' THEN t.value ELSE NULL END) AS A,
         MAX(CASE WHEN t.col = 'B' THEN t.value ELSE NULL END) AS B
    FROM TABLE t
GROUP BY t.id

MySQL doesn't support PIVOT/UNPIVOT syntax.

OMG Ponies
I have used pivot table, but I have one more issue. The column A and B must be create automaticly. Like ThisFor each t.col Print it on horizontal line, then their prices on column!
Calebe
@Calebe: "automatically"?
OMG Ponies
A: 

Assuming that your table is "table_name", the first column is "id", the second column is "Value", the third column is "Type" and is an enum with 'A' or 'B':

SELECT
    a.ID,
    a.Value,
    b.Value
FROM table_name AS a,
    table_name AS b
WHERE a.ID=b.ID AND
    a.Type='A' AND
    b.Type='B'
EboMike