将数据行转换为类似于pivot但具有实际值的表

时间:2013-01-17 12:25:13

标签: mysql excel pivot-table

我在mysql表格中有数据格式为

C1 | C2 | C3 (Column titles)  
A1 | X1 | Y1  
A1 | X2 | Y2  
A1 | X3 | Y3  
B1 | X1 | Y4  
B1 | X2 | Y5  
B1 | X3 | Y6  
B1 | X4 | Y7  
C1 | X1 | Y8  
C1 | X2 | Y9  

我想将此转换为

00 | X1 | X2 | X3 | X4  
A1 | Y1 | Y2 | Y3 |   
B1 | Y4 | Y5 | Y6 | Y7  
C1 | Y8 | Y9

非常类似于数据透视表,但是将Yx的实际值放在表中而不是对它们执行操作(Sum / Avg等)。

在mysql或excel / libreoffice中有一种简单的方法吗?

谢谢

1 个答案:

答案 0 :(得分:2)

SQLFiddle demo

select 
C1,
max(case when c2='X1' then C3 else '' end) as `X1`,
max(case when c2='X2' then C3 else '' end) as `X2`,
max(case when c2='X3' then C3 else '' end) as `X3`,
max(case when c2='X4' then C3 else '' end) as `X4`

from t
group by C1