MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 5th, 2012, 08:56 AM
flippom flippom is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 flippom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 2 sec
Reputation Power: 0
Newbie needing help. Capture most current unique records

Here is my issue:
urn visit_no last_name first_name admit_date dis_date
3105 05424 MOUSE MICKEY 2012-11-15 2012-11-16
3105 06261 MOUSE MICKEY 2012-11-26 2012-11-28
3105 00469 MOUSE MICKEY 2011-11-01 2012-11-03
4204 00047 DUCK DAFFY 2010-12-01 2010-12-05
4204 00090 DUCK DAFFY 2010-07-01 2010-07-06
4204 06300 DUCK DAFFY 2012-12-01 2012-12-03


select P.URN, V.VISIT_NO, P.LAST_NAME, P.FIRST_NAME, V.ADMIT_DATE, V.DISCH_DATE

from PATIENT P
inner join VISIT V on P.URN = V.URN

What I want to end up with is the record with the last admit_date for the unique urn

3105 06261 MOUSE MICKEY 2012-11-26 2012-11-28
4204 06300 DUCK DAFFY 2012-12-01 2012-12-03


Thanks for any help/sugguestions

Reply With Quote
  #2  
Old December 6th, 2012, 06:23 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,356 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 4 h 30 m 28 sec
Reputation Power: 4140
Code:
SELECT p.urn
     , v.visit_no
     , p.last_name
     , p.first_name
     , v.admit_date
     , v.disch_date
  FROM patient AS p
INNER 
  JOIN ( SELECT urn
              , MAX(admit_date) AS last_admit_date
           FROM visit 
         GROUP
             BY urn ) AS m 
    ON m.urn = p.urn
INNER 
  JOIN visit AS v 
    ON v.urn = m.urn
   AND v.admit_date = m.last_admit_date
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old December 6th, 2012, 08:32 AM
flippom flippom is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 flippom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 2 sec
Reputation Power: 0
Perfect - You and the Code

Thanks so much. This go me exactly what I was hoping for.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Newbie needing help. Capture most current unique records

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap