Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

SQL-- Max of count

Posted on 2015-01-30
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.
Question by:BBRRGG
  • 5
  • 3
LVL 65

Expert Comment

by:Jim Horn
ID: 40580989
No attachment

Author Comment

ID: 40580995
File now attached.

Author Comment

ID: 40581060
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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.

Author Comment

ID: 40581077

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
LVL 10

Accepted Solution

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


Basically using a couple inner queries with a having clause

Author Comment

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.
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

Author Closing Comment

ID: 40581101
Great, thanks!

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
backup and restore 21 29
CDC and AOG on MS SQL 2012 13 24
Parse this column 6 27
Stored Procedure needs owner to execute 5 19
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

861 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