34,873
社区成员
发帖
与我相关
我的任务
分享
if object_id('f') is not null drop table f
go
create table f (
Id int ,Name varchar(10))
insert f
select 1 ,'张三' union all
select 2 ,'李四'
if object_id('c') is not null drop table c
go
create table c (
Owner int,Name varchar(10),DateAdd int)
insert c
select 1 ,'语文', 2009 union all
select 2 ,'数学', 2009 union all
select 1 ,'英语', 2010
go
select * from c a,f b
where a.Owner=b.Id
and (select count(1) from c where Owner=a.Owner and DateAdd>a.DateAdd)=0
/*
Owner Name DateAdd Id Name
----------- ---------- ----------- ----------- ----------
1 英语 2010 1 张三
2 数学 2009 2 李四
(所影响的行数为 2 行)
*/select * from(
select *,rn=row_number()over(partition by c.Owner order by getdate()) from c,f where c.Owner=f.Id
) t where rn=1