34,571
社区成员
发帖
与我相关
我的任务
分享
select name,a.id,no,data,age
from
(select * from ta e where not exists(select 1 from ta where id = e.id and no > e.no)) a
left join
tb b on a.id = b.id
left join
tc c on a.id = c.id
--> 测试数据: @s
declare @s table (Id int,NO int,Data varchar(1))
insert into @s
select 1,1,'a' union all
select 1,2,'b' union all
select 2,1,'b' union all
select 2,2,'a' union all
select 2,3,'d'
--> 测试数据: @B
declare @B table (Id int,Name varchar(5))
insert into @B
select 1,'zhang' union all
select 2,'zou'
--> 测试数据: @c
declare @c table (id int,age int)
insert into @c
select 1,50 union all
select 2,50
select name,b.id,a.no,a.data,age from @b b
left join (select * from @s a where not exists(select 1 from @s where id=a.id and no>a.no))a
on a.id=b.id
left join @c c
on a.id=c.id
where t.id=b.id and t.id=c.id
select name,a.id,no,data,age
from
(select * from ta e where not exists(select 1 from ta where id = e.id and no > e.no) a
left join
tb b on a.id = b.id
left join
tc c on a.id = c.id
select b.name,t.no,t.data,c.age from b,c,(select id,data,max(no) as no from A group by id,data) as t
where a.id = b.id and a.id = c.id