If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Other > max(count(*)) question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-26-04, 12:14
weaselboy1976 weaselboy1976 is offline
Registered User
 
Join Date: Feb 2004
Posts: 18
max(count(*)) question

Hello

I would like to get the rows with the maximum number rows of each group of fields.

For example, I have a table called "sometable", with columns "x" and "y". It looks like this:

x y
1 77
1 77
1 22
1 44
1 44
1 44
2 66
2 99
2 33
2 33

So, I'd like to do:

select x, y, count(*)
from sometable
group by x, y
having count(*) = max(count(*));

And yield a result of 2 rows:

x y count
1 44 3
2 33 2

But of course, Ingres won't let me embedd aggregate functions like that. So how can I do this? A solution in ANSI SQL would be great! Answers using Ingres-specific features are acceptable too.

Thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 11-05-04, 09:52
amcamx amcamx is offline
Registered User
 
Join Date: Aug 2004
Posts: 28
I wanna help but I am not quite clear on what you are trying to do. Do you only wnat the TOP 2 with the highest count or do you want the ones with their count being greater than a given value ?


AmcAmx
Reply With Quote
  #3 (permalink)  
Old 11-05-04, 11:31
dbabren dbabren is offline
Registered User
 
Join Date: Jul 2003
Location: England
Posts: 152
select x, y, count(*)
from sometable
group by x, y
order by col3 desc

will give you a list in descending order of the number of occurances of x and y.

Not sure if this is what you are after though?
__________________
Regards
Dbabren
Reply With Quote
  #4 (permalink)  
Old 11-05-04, 12:03
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,613
I'd suggest:
Code:
SELECT x, y, count(*)
   FROM sometable
   GROUP BY x, y
   HAVING Count(*) = (SELECT Max(Count(*))
      FROM sometable
      GROUP BY x, y);
-PatP
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

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