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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Top 3 with Group by 2

Status
Not open for further replies.

MapMan

Programmer
Jul 21, 1999
143
US
Here's my delemna:<br>
<br>
I need to grab no more than three records from a table per company in a table that meets a certain criteria.<br>
<br>
Here's a table to use for this question. <br>
<br>
((I can't figure out how to get this to show in a formatted output, sorry))<br>
<br>
CONTACT<br>
NAME TITLE COMPANY<br>
-------------------------- ----------------- ---------------------<br>
JOHN SMITH PRESIDENT DETROIT MOTORS, INC.<br>
DAVID BUEHLER VICE-PRESIDENT DETROIT MOTORS, INC.<br>
BOB WHEELER CHAIRMAN DETROIT MOTORS, INC.<br>
STAN RECORD VICE-PRESIDENT DETROIT MOTORS, INC.<br>
CATHY PAGE CHAIRMAN BOOK INN<br>
DANIEL TARIN PRESIDENT BOOK INN<br>
ERIC LEAVENWORTH VICE PRESIDENT BOOK INN<br>
RICHARD BURNHAM CIO BOOK INN<br>
…<br>
DANIEL PELTON CEO BOOK INN<br>
<br>
I need to write a query from the CONTACT table that will show no more than 3 contacts per company that meet the chief contact criteria.<br>
<br>
Here's what I've got but, it's not working.<br>
<br>
select C.Name, C.Title, C.Company from Contact C<br>
where Exists (Select * from contact <br>
where ( title like &quot;%pres%&quot; or<br>
title like &quot;%CIO%&quot; or<br>
title like &quot;%CEO&quot; or<br>
title like &quot;%CHAIR%&quot;)<br>
Group by Company)<br>
<br>
<br>
Any ideas?<br>
<br>
MapMan<br>

 
What RDBMS are you using?<br>
<br>
IN SQL Server there is a TOP keyword, so you would use:<br>
<br>
select TOP 3 C.Name, C.Title, C.Company from Contact C<br>
where Exists (Select * from contact <br>
where ( title like &quot;%pres%&quot; or<br>
title like &quot;%CIO%&quot; or<br>
title like &quot;%CEO&quot; or<br>
title like &quot;%CHAIR%&quot;)<br>
Group by Company)<br>
<br>
Cal<br>

 
and in oracle there's a hidden column in every table returned by sql called rownum - so you'd just add a where clause &quot;and rownum &lt;= 3&quot;<br>
<br>
-ml<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
I've tried calahans method. But that only brings up 3 records. <br>
<br>
What I'm trying to do is get up to three records per company in CONTACT. If my table has 50 records but only two companies, I am looking for 6 or fewer records overall.<br>
<br>
Is that even possible?<br>
<br>

 
MapMan -<br>
Try a correlated subquery:<br>
<br>
SELECT outer.Name, outer.Title, outer.company <br>
FROM contact outer<br>
WHERE outer.rowid IN <br>
(SELECT inner.rowid FROM contact inner<br>
WHERE outer.company = inner.company<br>
AND rownum &lt; 4)<br>
ORDER BY outer.company;<br>
<br>
This will get you at most three contacts per company. You will have to expand on the subquery if there is a requirement for three specific contacts.<br>

 
Carp --<br>
<br>
Now I know why I'd rather be using Oracle -vs- MSS 7.0. The latter, from what I can tell, doesn't have the rownum function. :(<br>
<br>
I'm still working on it and really appreciate your help guys.<br>
<br>
Thanks,<br>
<br>
MapMan
 
Based on calahans post you might be able to do the same thing like this:<br>
<br>
<br>
<br>
SELECT outer.Name, outer.Title, outer.company <br>
FROM contact outer<br>
WHERE outer.name IN <br>
(SELECT TOP 3 inner.name FROM contact inner<br>
WHERE outer.company = inner.company<br>
)<br>
ORDER BY outer.company;<br>
<br>
This avoids relying on rowid or rownum and uses the TOP function in sql server.
 
Perfect!!!!<br>
<br>
This was right on the money and I can now get some sleep tonight.<br>
<br>
Thank you,<br>
<br>
MapMan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top