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 2 but grouped

Status
Not open for further replies.

UHNSTrust

Technical User
Dec 2, 2003
262
GB
I have had a look around the forum for this and there are lots of examples but I am struggling to understand them. I am hoping that if I use my own data then I will be able to understand it better.

I have a table called 'tbl_Drugs_Prescribed'. This table contains 15 fields of which I only need two ('costcentre' and 'drug_name'). I want to be able to show the top 2 drugs prescribed (a simple count of the times a drug name appears) by cost centre.

So I have

costcentre drug_name
RJD DrugA
RJD DrugA
RJD DrugA
RJD DrugB
RJD DrugB
RJD DrugC
CCH DrugB
CCH DrugB
CCH DrugA
CCH DrugC
CCH DrugC
CCH DrugC

I want to be able to show

costcentre drug_name Count
RJD DrugA 3
RJD DrugB 2
CCH DrugC 3
CCH DrugB 2

I could do this in VBA but that would be slower. Can somebody show me how to do this efficiently through a query?

Thanks in advance
Jonathan
 
I have worked this out (I think!). However I would appreciate any advice on speeding it up as it is taking 15 minutes to run.

my query;
Code:
SELECT CCC.costcentre, CCC.drug_name, CCC.NoPrescribed
FROM sel_CountsByCC AS CCC
WHERE (((CCC.NoPrescribed) In (SELECT TOP 30 NoPrescribed FROM sel_CountsByCC WHERE costcentre=CCC.costcentre ORDER BY 1 DESC)));

Thanks if you had spent any time looking at this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top