SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 RETRIEVING LASTEST RECORDS
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

stevenandler
Starting Member

USA
42 Posts

Posted - 10/10/2012 :  15:57:26  Show Profile  Reply with Quote
I am trying to create a select query that will pull the latest record for each patient only. For instance, if the table has the following rows:

PATIENT_ID MRN LASTNAME FIRSTNAME
1234 6767 BROWN CHARLIE
2345 6767 BROWN CHARLIE
3456 6767 BROWN CHARLIE


I would like my select statement to return only the third row. I don't think the Distint Key word can work in this scenario because there are other columns with different date and time stamps for each row.

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 10/10/2012 :  16:16:13  Show Profile  Reply with Quote
quote:
Originally posted by stevenandler

I am trying to create a select query that will pull the latest record for each patient only. For instance, if the table has the following rows:

PATIENT_ID MRN LASTNAME FIRSTNAME
1234 6767 BROWN CHARLIE
2345 6767 BROWN CHARLIE
3456 6767 BROWN CHARLIE


I would like my select statement to return only the third row. I don't think the Distint Key word can work in this scenario because there are other columns with different date and time stamps for each row.


Is that [MRN] column the patient identifier? Your [PATIENT_ID] values are different for each Charlie Brown.

But at any rate one way is to identify which of your date columns represents the sequence of which you want the "last" row. Then select the max(<date>) grouped by a patient identifier. Then turn that statement into a derived table and join to the main table by patient identifier and that date column.
ie:

select yt.*
from   (
       select patient_id, max(dt) as dt
       from yourTable
       ) d
join   youTable yt
       on yt.patient_id = d.patient_id
       and yt.dt = d.dt


Be One with the Optimizer
TG
Go to Top of Page

stevenandler
Starting Member

USA
42 Posts

Posted - 10/10/2012 :  16:35:59  Show Profile  Reply with Quote
My apology. I didn't provide enough infomation.

The primary keys for the table OGEN.GEN_M_PATIENT_MAST are
FACILITY_KEY AND PATIENT_ID. I am trying to perform a select from this table by specifying the FACILITY_KEY in a where statement. Since the table contains many rows with the same patient, I am trying to retrive only the latest entry which can be determined by the PATIENT_ID number. The higher the number, the more recent the entry.
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 10/10/2012 :  17:44:24  Show Profile  Reply with Quote
>>The higher the number, the more recent the entry.
Then use that number instead of [dt] in my example. If you still need help then post actual DDL and a few rows of DML.

Be One with the Optimizer
TG
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000