Bhupinder Singh
Bhupinder Singh

Reputation: 1071

creating a special view or function

I have a table A (ID int, codeID int, sdate datetime, edate datetime, SID int)

I want to insert only those records in a view which have most recent edate for same codeID,

example

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

Now in view or function, I want only

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

because there is updated value for codeID 25 (expired however) but I still want it.

Thanks

Upvotes: 0

Views: 31

Answers (1)

Isaac
Isaac

Reputation: 2721

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

See it in action here.

Upvotes: 1

Related Questions