?
Solved

Get the earliest record in a table for each patient

Posted on 2011-02-16
6
Medium Priority
?
817 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
[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
  • Learn & ask questions
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 41

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 57

Accepted Solution

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

Open in new window

0
 
LVL 50

Expert Comment

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

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

801 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