Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 MS Access
 Easy SQL question?

Author  Topic 

synapse5150
Starting Member

2 Posts

Posted - 2009-07-09 : 09:49:45
This should be an easy SQL question, but I can't get it with my limited SQL knowledge. I have a table with 3 fields: PERSONID, CONTACTID, and ORDERID. I need to pull the top 3 ORDERID's for each distinct PERSONID.
I would appreciate any help I can get with this, and I thank you all in advance!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-09 : 11:57:44
Top 3 according to what? I'll assume the three latest orders, if that's ok with you.
The performance will be awful, but it will work

SELECT t1.PersonID, t1.OrderID
FROM Table1 AS t1
WHERE t1.OrderID IN (SELECT TOP 3 x.OrderID FROM Table1 AS x WHERE x.PersonID = t1.PersonID ORDER BY x.OrderDate DESC)



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

synapse5150
Starting Member

2 Posts

Posted - 2009-07-09 : 12:12:15
Perfect! I had to do a little editing, but that's exactly what I needed. Thanks again for your help!
Go to Top of Page
   

- Advertisement -