Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit number of associated records 1

Status
Not open for further replies.

kadochnikov

Technical User
Apr 20, 2001
2
US
I am joining two databases together and pulling “products” from one and “customers” from another. I would like to have all products, but only 3 customers per product? How can I limit these customers?
 

I don't know the exact structure of your tables nor the relationship between them. However, you should be able to use the following as a starting point.

SELECT p.ProdID, p.Desc, C.CustID, c.CustName
FROM Products As p INNER JOIN Customers As c
ON p.ProdId=C.ProdID
WHERE c.CustID In (Select Top 3 CustID From Customers Where ProdId=p.ProdID);

The correlated sub query selects the 3 records from the customer table for each ProdID. The outer query selects only the customers that are selected by the sub query. Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
Thanks a lot, I realy do not understand how it works, but it worked perfectly!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top