|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
Aggregate fucntions
Not sure if this is the correct forum for my postig, but here is my problem. I have two different tables, golf_course and outing, I need to write a query that will display the name of the golf course with the most total outings. I did the following:
SELECT gc.name, COUNT(o.course_id) AS total_outings FROM golf_course gc, outing o WHERE gc.course_id = o.course_id; My first error is: ERROR at line 1: ORA-00937: not a single-group group function. If I change it to: SELECT gc.name, COUNT(o.course_id) AS total_outings FROM golf_course gc, outing o WHERE gc.course_id = o.course_id GROUP BY gc.name; I get the names of all the golf courses. I only need to get the one with the most outings. I was wondering if i can use the MAX function to accomplish this, I tried something like MAX(COUNT(gc.course_id)), but like I expected it didn't work. Can anyone help? I'm using sqlplus. Thanks. |
|
#2
|
||||
|
||||
|
You can get the first of an ordered list with rank with:
Code:
SELECT * FROM (SELECT ename, sal, rank() OVER (ORDER BY sal DESC) "RANK" FROM scott.emp) WHERE RANK = 1 Code:
SELECT ename, sal FROM scott.emp WHERE sal = (SELECT max(sal) FROM scott.emp) Code:
SELECT * FROM (SELECT ename, sal FROM scott.emp ORDER BY sal DESC) WHERE rownum = 1
__________________
My blog about OpenSource Databases PDF tutorials about OSS databases, DBMonster ... Please contribute to Open Source Development, fill bug reports!!! Developer Shed eSupport Commented my.ini/my.cnf (PLEASE ADD YOUR OWN CONFIG TRICK) An introduction to database normalization Natural or Surrogate key Custom ordering for your results Correlated and uncorrelated subqueries Don't turn your outer joins into inner joins Last edited by pabloj : July 9th, 2004 at 11:32 AM. |
|
#3
|
|||
|
|||
|
If you want to try another option, look at the following query:
SELECT deptno, count(*) FROM emp GROUP BY deptno HAVING count(*) IN (SELECT max(count(*)) FROM emp GROUP BY deptno) / |
|
#4
|
|||
|
|||
|
Thanks for your answers. I tried the second query
SELECT gc.name, COUNT(o.course_id) AS total_outings FROM golf_course gc, outing o WHERE gc.course_id = o.course_id GROUP BY gc.name HAVING COUNT(o.course_id) IN (SELECT MAX(COUNT(o.course_id)) FROM outing o GROUP BY gc.name); but I didn't get any results. "no rows selected" I'll try the other one and see what happens. Thanks again |
|
#5
|
|||
|
|||
|
Execute the following query:
SELECT gc.name, COUNT(o.course_id) AS total_outings FROM golf_course gc, outing o WHERE gc.course_id = o.course_id GROUP BY gc.name HAVING COUNT(o.course_id) = (SELECT MAX(COUNT(o.course_id)) FROM outing o GROUP BY o.course_id); |
|
#6
|
|||
|
|||
|
That worked great, thanks a lot for your help.
|
![]() |
| Viewing: Dev Shed Forums > Databases > Oracle Development > Aggregate fucntions |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|