--估计是这样
create table tb (a int ,b int ,c varchar(10))
insert into tb select 1 , 0 , 'A'
insert into tb select 2 , 1 , 'aa'
insert into tb select 3 , 1 , 'bb'
insert into tb select 4 , 2 , 'aa1'
insert into tb select 5 , 2 , 'aa2'
insert into tb select 6 , 2 , 'aa3'
insert into tb select 7 , 3 , 'bb1'
goselect a , b ,
c1 = case when b = 1 then c else '' end ,
c2 = case when b <> 1 then c else '' end
from tbdrop table tb/*
a b c1 c2
----------- ----------- ---------- ----------
1 0 A
2 1 aa
3 1 bb
4 2 aa1
5 2 aa2
6 2 aa3
7 3 bb1(7 行受影响)
*/
create table tb (a int ,b int ,c varchar(10))
insert into tb select 1 , 0 , 'A'
insert into tb select 2 , 1 , 'aa'
insert into tb select 3 , 1 , 'bb'
insert into tb select 4 , 2 , 'aa1'
insert into tb select 5 , 2 , 'aa2'
insert into tb select 6 , 2 , 'aa3'
insert into tb select 7 , 3 , 'bb1'
goselect a , b ,
c1 = case when b = 1 then c else '' end ,
c2 = case when b <> 1 then c else '' end
from tbdrop table tb/*
a b c1 c2
----------- ----------- ---------- ----------
1 0 A
2 1 aa
3 1 bb
4 2 aa1
5 2 aa2
6 2 aa3
7 3 bb1(7 行受影响)
*/
--把C列拆为两列
select
a,
b,
[C1]=case when b=1 then c else '' end,
[C2]=case when b!=1 then c else '' end
from
@t
a,
b,
case when b=1 then c else '' end as c1,
case when b=1 then '' else c end as c2
from 表名