?
Solved

SQL-- Max of count

Posted on 2015-01-30
9
Medium Priority
?
408 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 66

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 11

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 11

Accepted Solution

by:
Jeffrey Dake earned 2000 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 11

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

839 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