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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Join Tables with limit
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 12/30/2012 :  00:48:43  Show Profile  Reply with Quote
I have 3 tables and need to do a join with the 3 but with a limit for one of them.
Lets say:
Table Persons
Table Cars
Table Childs

I want to see a line for every combination Person / Child / Car (but only for the last 4 cars)

This can be the query for all combinations

select,, from Persons p, Childs c, Cars r where and

but now the question is how can I limit this to show only combinations for the most recent 4 for that Person?

Flowing Fount of Yak Knowledge

7174 Posts

Posted - 12/30/2012 :  10:01:05  Show Profile  Reply with Quote
Select,,,ROW_NUMBER() OVER (PARTITION BY P.Name Order by desc) as Seq
from Persons p
inner join Childs c on = c.parentid 
inner join Cars r on = r.ownerid
Where T.Seq <=4
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000