同事今天中午去参加一个面试,有一道面试题比较基础
把
a b c
1 1 1.1
1 2 1.2
1 3 1.3
2 1 2.1
2 2 2.2
2 3 2.3
变成
A B1 B2 B3
1 1.1 1.2 1.3
2 2.1 2.2 2.3
思考了一下,得出结果
SELECT a as A,
max(CASE WHEN b='1' THEN c END) as B1,
max(CASE WHEN b='2' THEN c END) as B2,
max(CASE WHEN b='3' THEN c END) as B3
FROM test
GROUP BY a;