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

Top 5 per Selection

Status
Not open for further replies.

Noah114

Technical User
Mar 31, 2005
59
0
0
US
I have two tables, one with a table for physicians with totals sales, the other, a breakout of the physician sales by drug. Is there a way to create a query that will break out the physicians, list their top 5 products, and then an all other category that will match their total?
 
Code:
select p.id
     , p.total_sales
     , ps1.sales_amount
  from physicians as p
inner
  join physician_sales as ps1
    on ps1.physician_id = p.id
inner
  join physician_sales as ps2
    on ps2.physician_id = p.id
   and ps2.sales_amount > ps1.sales_amount
group
    by p.id
     , p.total_sales
     , ps.sales_amount
having count(*) < 5

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top