一种方法是,既然您在2005年及以后使用的是2000,那么您也可以使用行号()。
SELECT t1.*
from AreaCodes t1
INNER JOIN (SELECT NPANXX, max(EFFCHANGE) as MaxDate FROM AREACODES
group by NPANXX) t2
ON t1.NPANXX = t2.NPANXX
and t1.EFFCHANGE = t2.MaxDate
这是完整的代码,包括DML和DDL
create table AreaCodes(NPANXX int,TZONE int,EFFCHANGE datetime,RATE int)
insert AreaCodes values(555555,1,'20090101',1)
insert AreaCodes values(555555,1,'20090501',6)
insert AreaCodes values(214555,2,'20090101',1)
insert AreaCodes values(214555,2,'20090501',3)
insert AreaCodes values(657555,3,'20090501',1)
insert AreaCodes values(657555,1,'20090101',1)
SELECT t1.*
from AreaCodes t1
INNER JOIN (SELECT NPANXX, max(EFFCHANGE) as MaxDate FROM AREACODES
group by NPANXX) t2
ON t1.NPANXX = t2.NPANXX
and t1.EFFCHANGE = t2.MaxDate
output
657555 3 2009-05-01 00:00:00.000 1
555555 1 2009-05-01 00:00:00.000 6
214555 2 2009-05-01 00:00:00.000 3