28,390
社区成员
发帖
与我相关
我的任务
分享
declare @tb1 table (aid int identity,a nvarchar(50),b nvarchar(50),c nvarchar(50),ano int)
declare @tb2 table (bid int identity,d nvarchar(50),e nvarchar(50),f nvarchar(50),bno int)
insert into @tb1 (a,b,c,ano) values(1,1,1,1),(2,2,2,2),(1,2,3,1)
insert into @tb2 (d,e,f,bno) values(1,1,1,1),(2,2,2,2),(1,2,3,3)
select * from (select * from @tb1 where aid in (select max(aid) from @tb1 group by ano)) a inner join (select * from @tb2 where bid in (select max(bid) from @tb2 group by bno)) b on a.ano=b.bno order by ano
关键是你所说的重复需要按什么样的规则去除
最好你把现在搜索到的记录列一个出来,然后把期望的结果列出来,这样别人才能明白你到底想干什么declare @tb1 table (aid int identity,a nvarchar(50),b nvarchar(50),c nvarchar(50),ano int)
declare @tb2 table (bid int identity,d nvarchar(50),e nvarchar(50),f nvarchar(50),bno int)
insert into @tb1 (a,b,c,ano) values(1,1,1,1),(2,2,2,2),(1,2,3,1)
insert into @tb2 (d,e,f,bno) values(1,1,1,1),(2,2,2,2),(1,2,3,3)
select Distinct * from @tb1 a inner join @tb2 b on a.ano=b.bno order by ano
不明白你说的重复是什么,是要 ano,bno相同的也只出现一次么?