创建一个特殊的视图或功能

时间:2012-11-30 19:56:07

标签: sql function view

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

我想只在视图中插入那些具有相同代码ID

的最新编辑的记录

例如

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有更新的值(但已过期),但我仍然想要它。

由于

1 个答案:

答案 0 :(得分:1)

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

在行动here中查看。