我有id, name
列,如下所示
id name
1 A
1 B
1 C
1 D
1 E
1 F
2 A
2 B
2 C
我想显示像
这样的数据ID A B C D E F G
1 Y Y Y Y Y Y N
2 Y Y Y N N N N
其中:
Y
= 是 - EXISTS
N
= 否 - 做NOT EXIST
答案 0 :(得分:1)
SELECT
id,
MAX( CASE when name = 'A' THEN 'Y' ELSE 'N' END ) A,
MAX( CASE when name = 'B' THEN 'Y' ELSE 'N' END ) B,
MAX( CASE when name = 'C' THEN 'Y' ELSE 'N' END ) C,
MAX( CASE when name = 'D' THEN 'Y' ELSE 'N' END ) D,
MAX( CASE when name = 'E' THEN 'Y' ELSE 'N' END ) E,
MAX( CASE when name = 'F' THEN 'Y' ELSE 'N' END ) F,
MAX( CASE when name = 'G' THEN 'Y' ELSE 'N' END ) G
from
yourTable yt
group by id