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


How to get MAX value record?


How to get MAX value record?

Author
Message
justin.walker 2622
justin.walker 2622
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 6
My dataset looks like this:

Provider Patient Visits
Dr. A John Smith 1
Dr. B John Smith 2
Dr. C John Smith 1
Dr. D John Smith 1
Dr. E Suzy Q 5
Dr. B Suzy Q 3
Dr. A Suzy Q 4

I want to return a single distinct record for each patient which will be the record with the highest visit count. How would I do this?
For my example the return set would be:

Provider Patient Visits
Dr. B John Smith 2
Dr. E Suzy Q 5
justin.walker 2622
justin.walker 2622
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 6
I figured it out. I came up with the following:

RANKING AS

(

SELECT

ROW_NUMBER() OVER(PARTITION BY d.Patient ORDER BY d.visits DESC) AS row_num,

d.Provider,

d.Patient,

d.visits

FROM DETAILS d

)

SELECT

*

FROM RANKING r

WHERE r.row_num = 1

ORDER BY r.Provider;


DesNorton
DesNorton
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19864 Visits: 8650
And how do you handle a tie? Where the patient sees multiple providers the same number of max times.


How to post data/code on a forum to get the best help.
Make sure that you include code in the appropriate IFCode tags.
justin.walker 2622
justin.walker 2622
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 6
Good question, I was just thinking about that too. I am going to bring in the last encounter date and choose the provider they most recently seen.
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