Solved

Get the earliest record in a table for each patient

Posted on 2011-02-16
6
812 Views
Last Modified: 2012-05-11
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
Comment
Question by:Wonderwall
6 Comments
 
LVL 75

Expert Comment

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

Author Comment

by:Wonderwall
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

by:Aneesh Retnakaran
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 40

Expert Comment

by:Sharath
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

Open in new window

0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 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

Open in new window

0
 
LVL 50

Expert Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql help 5 53
Table create permissions on SQL Server 2005 9 41
Help Required 2 33
date diff with Fiscal Calendar 4 31
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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