Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old July 9th, 2004, 10:18 AM
merck merck is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 13 merck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old July 9th, 2004, 11:28 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,711 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 6 Days 4 h 53 m 59 sec
Reputation Power: 259
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
or get the employee with the highest salary with
Code:
SELECT ename, sal FROM scott.emp WHERE sal = (SELECT max(sal) FROM scott.emp)
or (but this will get always only 1 result)
Code:
SELECT * FROM (SELECT ename, sal FROM scott.emp ORDER BY sal DESC) WHERE rownum = 1
Hope this helps

Last edited by pabloj : July 9th, 2004 at 11:32 AM.

Reply With Quote
  #3  
Old July 9th, 2004, 12:41 PM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
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)
/

Reply With Quote
  #4  
Old July 9th, 2004, 02:18 PM
merck merck is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 13 merck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #5  
Old July 9th, 2004, 07:47 PM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 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);

Reply With Quote
  #6  
Old July 11th, 2004, 04:57 PM
merck merck is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 13 merck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
That worked great, thanks a lot for your help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Aggregate fucntions


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway