SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Selecting very specific records ...


Selecting very specific records ...

Author
Message
mikeallenbrown
mikeallenbrown
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 11
In another thread I had a query where I needed to select only patient new admissions. The query finds the first entry per patient (per SSN actually) and ignores the rest. You can see that here: http://www.sqlservercentral.com/Forums/Topic1421364-1292-1.aspx

What I need now is to select only re-admissions. So basically excluding the FIRST entry for a patient. If a patient was entered only once then that record is not selected at all.

Here is the data:



Based off of the above data I'd expect to see ONLY line 5 and 7 .... patient with SSN of 123-12-3123 just happens to be the only duplicates in this sample but in a live database there could be many duplicates. Line 8 is the first admit so it too is excluded.

Does that make sense? ....please let me know if further clarification is need. I wouldn't consider myself an "advanced" SQL use but I'm learning fast! :-D

Thank you in advance.

-Mike
Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 6000 Visits: 5280
You can use ROW_NUMBER() to achieve the desired results

SELECT   *
FROM (
SELECT ROW_NUMBER() OVER ( PARTITION BY Client.ID, Patient.SSN ORDER BY StartOfCare ) AS RN,
Client.ID, Client.ClientName, Patient.ClientID, Patient.SSN, Patient.StartOfCare, Patient.PrimDiag
FROM Client Client
INNER JOIN Patient Patient ON Client.ID = Patient.ClientID
WHERE Patient.RptYear = 2012 AND Patient.PrimDiag <> 'NULL' AND Patient.PrimDiag <> '' AND Patient.SSN <> ''
) P
WHERE P.RN > 1




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
mikeallenbrown
mikeallenbrown
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 11
Thank you ... that did it!

-Mike
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search