Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Christopher.

Asked: December 03, 2004 - 9:46 am UTC

Last updated: December 06, 2004 - 12:28 pm UTC

Version: 9.2.5

Viewed 1000+ times

You Asked

Tom,

Here is what I am doing.

select interest_rate, count(interest_rate)
from volume_statistics where interest_rate between 16.01 and 18
and principal_amount between 10000.01 and 15000
group by interest_rate


INTEREST_RATE COUNT(INTEREST_RATE)
------------- --------------------
16.24 8
16.25 8
16.3 1
16.45 2
16.49 3
16.74 6
16.75 2
16.85 1
16.9 1
16.95 10
16.99 6

INTEREST_RATE COUNT(INTEREST_RATE)
------------- --------------------
17 1
17.24 5
17.25 3
17.35 1
17.45 3
17.49 8
17.74 3
17.75 8
17.85 3
17.94 1
17.95 84

INTEREST_RATE COUNT(INTEREST_RATE)
------------- --------------------
17.99 12
18 28

24 rows selected.


However what I really want the output to be is the highest count or frequency that an apr appears
and obtain that apr.
17.95 84


I tried the following, but get the following error, which makes sense:

select interest_rate, Max(count(interest_rate))
from volume_statistics where interest_rate between 16.01 and 18
and principal_amount between 10000.01 and 15000
group by interest_rate
ERROR at line 2:
ORA-00937: not a single-group group function

I also have to take in account if for some reason the highest counts are the same for 1 + n values.
In this example I would always want the higest interest rate which is 17.74:

INTEREST_RATE COUNT(INTEREST_RATE)
------------- --------------------
17.45 3
17.74 3

I figured I could just put it in a cursor and retrieve the first row if I sort it in DESC order,
however this is a report being run out of a cron via SQR. Plus, I figured you would have an
answer.

Any help would be great.
Thanks,
Chris


and Tom said...

Analytics rock
they roll
they slice bread
and make julian fries....


scott@ORA9IR2> select sal, count(*) ,
2 rank() over (order by count(*) DESC ) r
3 from emp group by sal
4 /

SAL COUNT(*) R
---------- ---------- ----------
1250 2 1
3000 2 1
800 1 3
950 1 3
1500 1 3
2450 1 3
5000 1 3
2975 1 3
2850 1 3
1600 1 3
1300 1 3
1100 1 3

12 rows selected.

scott@ORA9IR2>
scott@ORA9IR2>
scott@ORA9IR2> select *
2 from (
3 select sal, rank() over ( order by count(*) DESC ) r
4 from emp
5 group by sal
6 order by 2 desc
7 )
8 where r = 1
9 /

SAL R
---------- ----------
1250 1
3000 1


Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Awesome

Chris, December 03, 2004 - 1:26 pm UTC

Thanks for the response. That was exactally what I was looking for. It's great that analytics can be so simple at times, but produce such powerful results.

A reader, December 03, 2004 - 10:28 pm UTC

And to think, for all these years, I've been using a knife to cut bread.

Out of curiosity...

Dan Kefford, December 06, 2004 - 9:50 am UTC

... has any vendor ever considered implementing a MODE() function/analytic?

Tom Kyte
December 06, 2004 - 12:28 pm UTC

isn't it "rank=1 and count > 1"?

but the problem with MODE is that it natually returns a collection, the mode of

1,1,2,2

is
1 and 2

(eg: it is not scalar)

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.