Solved

Creating an SQL view that pulls the oldest record per claimid

Posted on 2007-12-04
4
897 Views
Last Modified: 2012-06-22
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.
0
Comment
Question by:boukaka
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
mastoo earned 500 total points
ID: 20405002
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
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20405053
Just a minor correction.  No points please:

Select mt.claimid, mt.historydate, mt.reserveamount
From mytable As mt
      Join (
            Select claimid, max( historydate ) As MaxHistory
            From mytable
            Group By claimid) As T2 On mt.claimid = T2.claimid And mt.historydate = T2.MaxHistory
0
 
LVL 21

Expert Comment

by:mastoo
ID: 20405116
Thanks.  Once I go beyond 2 lines of code there's bound to be an error :-)
0
 

Author Closing Comment

by:boukaka
ID: 31412641
Oh wow, you're a lifesaver! thank you SO much!!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now