Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2004
    Posts
    18

    Unanswered: 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!

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

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

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •