Sql 将单列数据转换为多行
我有一个只有3种服务类型的表,我只想透视服务类型列并显示Sql 将单列数据转换为多行,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有一个只有3种服务类型的表,我只想透视服务类型列并显示 ServiceID FName LName Servicetpe 1 A B ST1 1 A B ST2 2 g e st1 3 f h st1 3 f h st2 3 f h
ServiceID FName LName Servicetpe
1 A B ST1
1 A B ST2
2 g e st1
3 f h st1
3 f h st2
3 f h st3
输出应该是
ServiceID FName LName ST1 ST2 ST31
1 A B X X
2 g e X
3 f h X X X
我尝试了pivot,但无法使用out aggregate函数。您可以执行条件聚合:
select ServiceID, FName, LName,
max(case when Servicetpe = 'ST1' then 'X' end) ST1,
max(case when Servicetpe = 'ST2' then 'X' end) ST2,
max(case when Servicetpe = 'ST3' then 'X' end) ST3
from table t
group by ServiceID, FName, LName;