1

我在 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 中做到这一点?

谢谢

4

1 回答 1

2

SQLFiddle 演示

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
于 2013-01-17T12:32:59.070 回答