日期:2014-05-18 浏览次数:20454 次
create table A (aid int,bid int) insert into A values(1 ,1) insert into A values(2, 1) insert into A values(3, 1) insert into A values(4 ,2) insert into A values(5 ,2) create table B(bid int,bkey int) insert into B values(1, 1000) insert into B values(2 ,2000) insert into B values(3 ,3000) ;with ct as ( SELECT a.aid, b.bkey ,rn=ROW_NUMBER()over(partition by bkey order by getdate()) FROM A a join B b on a.bid=b.bid ) select * from ( select aid ,bkey from ct where rn=1 union all select aid ,'0' from ct where rn<>1)s order by aid drop table A drop table B aid bkey ----------- ----------- 1 1000 2 0 3 0 4 2000 5 0