日期:2014-05-18 浏览次数:20618 次
use test
go
create table #t1(ComId char(2),height char(2))
insert #t1 select 'A1','oo'
insert #t1 select 'A1','hh'
go
create table #t2(ComId char(2),width char(2))
insert #t2 select 'A1','T2'
insert #t2 select 'A1','T3'
insert #t2 select 'A1','T4'
go
select *,row=1 into #a from #t1 order by ComId
select *,row=1 into #b from #t2 order by ComId
go
declare @ComId char(2),@i int
update #a
set @i=case when ComId=@ComId then @i+1 else 1 end,row=@i,@ComId=ComId
set @i=0
update #b
set @i=case when ComId=@ComId then @i+1 else 1 end,row=@i,@ComId=ComId
go
select
[ComId]=case when a.row=(select min(row) from #b where ComId=b.ComId) then b.ComId else ''end,
[height]=isnull(a.height,''),
b.width
from
#b b
left join
#a a on b.ComId=a.ComId and b.row=a.row
/*
ComId height width
----- ------ -----
A1 oo T2
hh T3
T4
(所影响的行数为 3 行)
*/