declare @abc table( name nvarchar(20), thedate nvarchar(20) ) insert into @abc values('a','a2') insert into @abc values('b','b2') insert into @abc values('c','c2') insert into @abc values('b','b3') select * from (select ROW_NUMBER()over(partition by name order by thedate desc) as idd,name,thedate from @abc where thedate is not null )tt where tt.idd=1
像上面那样不会报错,可如果像下面这样就报错,可能是编译器不知道dd是哪个表的字段,无法编译所致,加了tt的话,编译器就知道idd是哪个表的字段了,可以编译。
select * from (select ROW_NUMBER()over(partition by name order by thedate desc) as idd,name,thedate from @abc where thedate is not null ) where idd=1
完。