Get the earliest record in a table for each patient

Posted on 2011-02-16
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

Question by:Wonderwall
LVL 75

Expert Comment

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

Author Comment

ID: 34912795
Sorry, forgot to add I need the status returned also so I can't use the group by  
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


;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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

LVL 40

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

Open in new window

LVL 51

Accepted Solution

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

LVL 50

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
T-SQL - Get Same Address1 with Different Address2 2 28
combine data in sql 1 29
Sql Permission 6 45
Problem with SqlConnection 5 124
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 this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

11 Experts available now in Live!

Get 1:1 Help Now