Solved

SQL-- Max of count

Posted on 2015-01-30
9
267 Views
Last Modified: 2015-01-30
Need SQL that provides the maximum number of items, & also the name of the maximum.

In the attached file, please provide SQL that outputs the following:

10  Fruit

This desired output is the max number of items in a category, and also that category name.
0
Comment
Question by:BBRRGG
  • 5
  • 3
9 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40580989
No attachment
0
 

Author Comment

by:BBRRGG
ID: 40580995
File now attached.
0
 

Author Comment

by:BBRRGG
ID: 40581060
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 10

Expert Comment

by:Jeffrey Dake
ID: 40581069
Select count(*)  , category  from table group by category order by count(*) desc;

This will give you all the counts, starting with the most.  I think that is what you are looking for.
0
 

Author Comment

by:BBRRGG
ID: 40581077
Almost.  

But later I need to do a Common Table Expression that joins on Category, and that join needs to filter the table to only fruit (or whichever category has the max # of items).  Thus, need to show only one record, which is the max category, such as:
10    Fruit
0
 
LVL 10

Accepted Solution

by:
Jeffrey Dake earned 500 total points
ID: 40581082
If that is the case I think the second example here should work for you.

http://www.w3resource.com/sql/aggregate-functions/max-count.php

Basically using a couple inner queries with a having clause
0
 

Author Comment

by:BBRRGG
ID: 40581084
To simplify my explanation, output needs to be only one record:

10  Fruit

Preferably one select statement, but if need a Common Table Expression to accomplish, that'd be OK too.
0
 
LVL 10

Expert Comment

by:Jeffrey Dake
ID: 40581088
Select category, count(*) from table group by category having count(*) = (select max(mycount) from (select category, count(*) from table group by category));

I think that is what you want. If not sorry I couldnt help
0
 

Author Closing Comment

by:BBRRGG
ID: 40581101
Great, thanks!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
best counters for cpu high usage 3 33
SQL Server Designer 19 44
date diff with Fiscal Calendar 4 32
SQL Query assistance 16 27
Read about achieving the basic levels of HRIS security in the workplace.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question