Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Get the earliest record in a table for each patient

Posted on 2011-02-16
Medium Priority
818 Views
Aloha I need to grab the earliest visit of a patient when they were flagged as a smoker.

the fields I have are MRN, Contact_Date and Status. I am having a brain cramp at the moment and drawing a blank. :-) mahalo

0
Question by:Wonderwall
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 75

Expert Comment

ID: 34912716
select mrn, max(contact_date) contactDate
from tableName
where status ='smoker'
group by mrn
0

Author Comment

ID: 34912795
Sorry, forgot to add I need the status returned also so I can't use the group by
0

LVL 75

Expert Comment

ID: 34912856
Not sure why you said that

select mrn, max(contact_date) contactDate ,status ='smoker'
from tableName
where status ='smoker'
group by mrn

or

;with cte as (
select mrn, contact_date, status, rn = row_number() over (partition by status order by contact_date desc )
from tableName
)
select  mrn, contact_date, status from cte where rn = 1
0

LVL 41

Expert Comment

ID: 34912894
``````select *
from (
select *,row_number() over (partition by mrn order by contact_date desc) rn
from your_table
where status = 'smoker') t1
where rn = 1
``````
0

LVL 60

Accepted Solution

HainKurt earned 2000 total points
ID: 34913091
I guess "earliest date" means we need to order by ASC
``````select * from (
select t.*, row_number() over (partition by mrn order by contact_date ASC) rn
from your_table
where Smoker = 'Y') x
where rn = 1
``````
0

LVL 50

Expert Comment

ID: 34915443
how do we know they are a smoker?
0

## Featured Post

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
###### Suggested Courses
Course of the Month8 days, 23 hours left to enroll