Q

The group with the greatest COUNT

I would like to retrieve the record that has the maximum number of counts (records) in the table. I have written a query using the MAX(COUNT(*)) but it doesn't work in SQL Server.

I would like to retrieve the record that has the maximum number of counts (records) in the table. I have written...

a query using the MAX(COUNT(*)) but it doesn't work in SQL Server.

select count(*) total, 
  a.fd_acct_account_no 
from payment_details_temp a 
group by a.fd_acct_account_no
having count(*) = 
  (select max(count(*)) 
  from payment_details_temp b 
  group by b.fd_acct_account_no ) 

Appreciate your support.

Since this is SQL Server, we can utilize the handy-dandy TOP keyword, which isn't standard SQL, but which serves the purpose very nicely:

select top 1
       count(*) total
     , fd_acct_account_no 
  from payment_details_temp  
group 
    by fd_acct_account_no
order 
    by count(*) desc

Simple, yes?

This was last published in September 2005

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close