Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Max Count(*) question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-20-04, 18:38
shoczak shoczak is offline
Registered User
 
Join Date: Oct 2004
Location: Ft. Lauderdale, FL
Posts: 1
Max Count(*) question

I have a max(count(*)) sort of question.

Table ACCOUNT_PRODUCTS has:

Code:
ACCOUNT_PRODUCT_ID, ACCOUNT_NUMBER, PRODUCT_ID


Table PRODUCTS has:

Code:
PRODUCT_ID, PRODUCT_NAME, PRODUCT_CLASS


I need to get back a result set of ACCOUNT_NUMBERS and the PRODUCT_CLASS that is the one that is represented most often for the ACCOUNT_NUMBER.

I.e.,

Acct1 has 3 products in Class "Q", 2 in Class "W" and 5 in Class "C"
Acct2 has 1 product in Class "V" and 1 in Class "S"

Results Set should pick first product class if the count is the same and return:

Code:
Acct1, C Acct2, V


I am baffled!

Thanks in advance,

Steve.
Reply With Quote
  #2 (permalink)  
Old 10-21-04, 06:24
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Break the problem into logical steps:

1) Get the counts by account and class:

Code:
select account, class, count(*) cnt from ... group by account, class;
2) Get the max count per account:

Code:
select account, max(cnt) maxcnt from ( select account, class, count(*) cnt from ... group by account, class ) group by account;
3) Get details of account and class for those max(cnt) values:

Code:
select account, class from ( select account, class, count(*) cnt from ... group by account, class ) where (account, cnt) in ( select account, max(cnt) maxcnt from ( select account, class, count(*) cnt from ... group by account, class ) group by account );
Now that does look messy. If your DBMS supports the WITH clause then you can rewrite as:

Code:
with temp as ( select account, class, count(*) cnt from ... group by account, class ) select account, class from temp where (account, cnt) in ( select account, max(cnt) maxcnt from temp group by account );
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On