|
|
Question : Creating an SQL view that pulls the oldest record per claimid
|
|
I'm having trouble creating a view that will pull the oldest record on each file in a table so that I can report on it. A sample of what the table looks like is below. What I need to do is capture the reserve amount of the oldest historydate for each claimid.
claimid historydate reserveamount 111222 Aug 1 2004 40.00 111222 Aug 2 2005 4000.00 111222 Sep 1 2007 365.40 333333 Jan 4 2006 1000.00 333333 Jan 6 2006 1040.00 999999 Nov 1 2007 5000.00 250000 Jan 20 2007 10.00 250000 Jan 21 2007 20.00 250000 Jan 22 2007 0.00
The result from the view on the above sample table should return this: claimid historydate reserveamount 111222 Aug 1 2004 40.00 333333 Jan 4 2006 1000.00 999999 Nov 1 2007 5000.00 250000 Jan 20 2007 10.00
Can anyone help me write this? I tried top(1)* but that just pulls the top one from the entire table not the top one per claimid.
|
Answer : Creating an SQL view that pulls the oldest record per claimid
|
|
Select mt.claimid, mt.historydate, mt.reserveamount From mytable As mt Join ( Select claimid, max( historydate ) As MaxHistory From mytable ) As T2 On mt.claimid = T2.claimid And mt.historydate = T2.MaxHistory
|
|
|
|