1

I'm trying to select max(count of rows). Here is my 2 variants of SELECT

SELECT MAX(COUNT_OF_ENROLEES_BY_SPEC) FROM
(SELECT D.SPECCODE, COUNT(D.ENROLEECODE) AS COUNT_OF_ENROLEES_BY_SPEC
 FROM DECLARER D
 GROUP BY D.SPECCODE
);

SELECT S.NAME, MAX(D.ENROLEECODE)
  FROM SPECIALIZATION S
    CROSS JOIN DECLARER D WHERE S.SPECCODE = D.SPECCODE
  GROUP BY S.NAME
  HAVING MAX(D.ENROLEECODE) = 
  ( SELECT MAX(COUNT_OF_ENROLEES_BY_SPEC) FROM
      ( SELECT D.SPECCODE, COUNT(D.ENROLEECODE) AS COUNT_OF_ENROLEES_BY_SPEC
        FROM DECLARER D
        GROUP BY D.SPECCODE
      )
  );

The first one is working OK, but I want to rewrite it using "HAVING" like in my second variant and add there one more column. But now 2nd variant don't output any data in results, just empty columns. How can I fix it ? Thank YOU!)

2
  • You want the maximum code equal a maximum number of codes? That doesn't look reasonable. Commented Apr 7, 2014 at 14:52
  • I want to count number of rows(declarations) grouped by specialization code, than take the most popular specialization. Sorry for my English;( Is it clear now ? Commented Apr 7, 2014 at 14:59

1 Answer 1

2

This query based on description given in comments and some suggestions, so it may be wrong:

select                   -- 4. Join selected codes with specializations
  S.Name,
  selected_codes.spec_code,
  selected_codes.count_of_enrolees_by_spec   
from 
  specialization S,
  ( 
    select               -- 3. Filter records with maximum popularity only
      spec_code, 
      count_of_enrolees_by_spec
    from (
      select             -- 2. Count maximum popularity in separate column
        spec_code, 
        count_of_enrolees_by_spec,
        max(count_of_enrolees_by_spec) over (partition by null) max_count
      from (
        SELECT           -- 1. Get list of declarations and count popularity
          D.SPECCODE           AS SPEC_CODE, 
          COUNT(D.ENROLEECODE) AS COUNT_OF_ENROLEES_BY_SPEC
        FROM DECLARER D
        GROUP BY D.SPECCODE
      )
    )
    where count_of_enrolees_by_spec = max_count
  )
    selected_codes
where 
  S.SPECCODE = selected_codes.spec_code

Also query not tested and some syntax errors are possible.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, it works witout mistakes! Can you, please, explain a little bit this part - "over (partition by null)"?
To use analytic functions you need to specify partitions in PARTITION BY clause to select group of rows like in GROUP BY. If you need only one group with all rows of dataset, it's useful to specify some constant expression. E.g. 1, 'X' and so on. Null also works. Personally, I prefer null for more code readability because it don't raise a questions like "Why choosed 1 and not 0?" and so on.
Thank You ! Great explanation. Can we use in this select GROUP BY expression instead of OVER (PARTITION BY NULL) to get same result ? I read about these expressions but I don't understand why I got error if I use max(count_of_enrolees_by_spec) and then trying to GROUP BY spec_code and count_of_enrolees_by_spec.
GROUP BY over full dataset (like GROUP BY 'x') is equivalent to just select max(COUNT_OF_ENROLEES_BY_SPEC) without any grouping. Each group must be identified uniquely and divides source record set to distinct groups without intersections. Therefore with one group you can't distinguish between different SPECCODEs and just got single value of maximum record count without ability to link it back to DECLARER or SPECIALIZATION tables.
From other side if you do GROUP BY over first (1.) query with some partitioning then you don't give maximum value.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.