22,210
社区成员
发帖
与我相关
我的任务
分享
select B.b1,B.b2,
(select case when B1 = 'A1' then (select A1 from A )
when B1 = 'A2' then (select A2 from A )
when B1 = 'A3' then (select A3 from A )
else null
end
from B
group by B1)
declare @A table (A1 int ,A2 int ,A3 int)
insert into @A
select 3,2,6
declare @B table(B1 CHAR(2),B2 nchar(1))
insert into @B
select 'A1','东' union all
select 'A2','南' union all
select 'A3','西'
select *,case when B1 = 'A1' then (select A1 from @A )
when B1 = 'A2' then (select A2 from @A )
when B1 = 'A3' then (select A3 from @A )
else null end as A
from @b
/*
(1 行受影响)
(3 行受影响)
B1 B2 A
---- ---- -----------
A1 东 3
A2 南 2
A3 西 6
(3 行受影响)
*/