0

我有一个表 A(ID int,codeID int,sdate datetime,edate datetime,SID int)

我只想在具有相同 codeID 的最新 edate 的视图中插入那些记录,

例子

ID codeID sdate        edate        SID
1  23     2011-01-01   2012-01-01   123
2  25     2007-01-01   2008-04-05   234
3  25     2008-07-08   2009-05-05   258
4  28     2007-05-05   NULL         987

现在在视图或功能中,我只想要

ID codeID sdate        edate        SID
1  23     2011-01-01   2012-01-01   123
3  25     2008-07-08   2009-05-05   258
4  28     2007-05-05   NULL         987

因为 codeID 25 有更新的值(但是已过期),但我仍然想要它。

谢谢

4

1 回答 1

1
select *
from A a1
where not exists (select 1 from A a2 where a1.codeID = a2.codeID and a2.edate > a1.edate)

在这里查看它的实际应用。

于 2012-11-30T19:58:26.300 回答