同事问的 也就随手一写而已。
use dbtest
if not object_id('Tb_ABC') is null
drop table Tb_ABC
create table Tb_ABC
( col01 varchar(100),
col02 varchar(100),
col03 varchar(100),
ColA varchar(100)
)
insert into Tb_ABC
select 'a','b','c','A'
union all
select 'a','b','c','B'
union all
select 'a','b','c','A'
union all
select 'a','b','c','B'
select * from Tb_ABC
/*
a b c A
a b c B
a b c A
a b c B
*/
要求转换后实现五列如下
/*
a b c A
a b c B
a b c A
a b c B
*/
select a.col01,a.col02,a.col03,
case a.colA when 'A' then a.colA else '' end,
case a.colA when 'B' then a.colA else '' end
from Tb_ABC a