SQL 2005 ... Select Top 1, 2 and 3rd

In my Query below, I get back two records in my dataset. My issue is I am only filtering on one relationship type in the table, however there are several other relationship types I am not concerned with pulling back. So in my test patient below, Its pulling one with a ListOrder = to 1 and one with ListOrder =5.

What I need is to pull the Top 1 (Max) based of the created date and and make that the first one. I then need the next most recent one created and make that #2 and then finally the third most recent one based off Created.

I hope this makes sense. If not, I can explain more.
SELECT
	PatientProfileId,
	pc.First,
	pc.Middle,
	pc.Last,
	pc.Phone1,
	ml.Description,
	pr.Listorder
FROM
	PatientRelationship pr 
	INNER JOIN PatientContacts pc ON pr.RelatedPartyId = pc.PatientContactsId 
	INNER JOIN Medlists ml ON pr.RelationshipTypeMId = ml.MedlistsId
WHERE
	pr.PatientProfileId = '672' AND
	pr.Type = 5  -- filter for personal contact type

Open in new window

LVL 7
Jeff SAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chapmandewCommented:
talking about this?

SELECT
      PatientProfileId,
      pc.First,
      pc.Middle,
      pc.Last,
      pc.Phone1,
      ml.Description,
      pr.Listorder,
ranking = row_number() over(order by create asc)
FROM
      PatientRelationship pr
      INNER JOIN PatientContacts pc ON pr.RelatedPartyId = pc.PatientContactsId
      INNER JOIN Medlists ml ON pr.RelationshipTypeMId = ml.MedlistsId
WHERE
      pr.PatientProfileId = '672' AND
      pr.Type = 5  -- filter for personal contact type

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeff SAuthor Commented:
chapmandew ...
Oooohhh I like this. Let me play with this some, but I think you got my friend! I will award points after I test it out some. You really are a life savior!
chapmandewCommented:
Sure...row_number() is a good one.  You can also check out rank(), dense_rank(), and ntile() for additional groupings if you'd like.
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Jeff SAuthor Commented:
These are new to me and I will need to read up on them. Thanks for the wonderful tips and teaching me. I love new tricks and I really appreciate you taking the time to help. Its very appreciated.
Jeff SAuthor Commented:
chap -
How would I filter this to just give me the one where ranking = 1?

SELECT
PatientProfileId,
pc.First,
pc.Middle,
pc.Last,
pc.Phone1,
ml.Description,
pr.Listorder,
ranking = row_number() over(order by pr.created asc)
FROM
PatientRelationship pr
INNER JOIN PatientContacts pc ON pr.RelatedPartyId = pc.PatientContactsId
INNER JOIN Medlists ml ON pr.RelationshipTypeMId = ml.MedlistsId
WHERE
pr.PatientProfileId = '672' AND
pr.Type = 5 -- filter for personal contact type
AND ranking = 1
gives me:
 
Msg 207, Level 16, State 1, Line 1
Invalid column name 'ranking'.
 
chapmandewCommented:
select * from (
SELECT
PatientProfileId,
pc.First,
pc.Middle,
pc.Last,
pc.Phone1,
ml.Description,
pr.Listorder,
ranking = row_number() over(order by pr.created asc)
FROM
PatientRelationship pr
INNER JOIN PatientContacts pc ON pr.RelatedPartyId = pc.PatientContactsId
INNER JOIN Medlists ml ON pr.RelationshipTypeMId = ml.MedlistsId
WHERE
pr.PatientProfileId = '672' AND
pr.Type = 5 -- filter for personal contact type
) a
where ranking = 1
Jeff SAuthor Commented:
SWEET. Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.