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:
  #1  
Old June 15th, 2005, 12:53 PM
rjett3345 rjett3345 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 21 rjett3345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 2 m 54 sec
Reputation Power: 0
SQL group by problems HELP

ok in my oracle class we have a problem that was given to us. He wants to the max average salary, grouped by departments AND the department number of the department it is in. Ive been getting errors all day and no one in my class has figured it out yet. anyway here is what i have.

select max(avg(salary)), department_id
from employees
group by department_id;

ive been getting errors about not being a single group group function.

i dont know how to do this one at all. Can anyone shed some light on how to get the max avg salary grouped by department #, AND the department number.

thanks

Reply With Quote
  #2  
Old June 15th, 2005, 01:26 PM
Catfish Jones's Avatar
Catfish Jones Catfish Jones is offline
Paranoid User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: Alberta, Canada
Posts: 340 Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level)Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level)Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level)Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level)Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level)Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 6 h 37 m 22 sec
Reputation Power: 32
max() will return just one result, the maximum. If I've read the problem correctly you'd want

select avg(salary), department_id
from employees
group by department_id
order by salary asc;

This should return one result for each department_id, ordered by their average salaries.

Edit: Nevermind, ignore that. I think my query will return the average over the entire table... anyways, this should really be in the Oracle forum, not the MySQL forum.

Reply With Quote
  #3  
Old June 15th, 2005, 01:34 PM
crazytrain81 crazytrain81 is offline
Always Learning
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2002
Location: Port Neches, TX, USA
Posts: 1,173 crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 7 h 45 m 43 sec
Reputation Power: 11
Send a message via AIM to crazytrain81 Send a message via MSN to crazytrain81
It will return the average by department, catfish, not the overall average. =)
__________________
David Fells
If my post helped you, please click the above my post and leave a comment. Thanks

Reply With Quote
  #4  
Old June 15th, 2005, 01:54 PM
Catfish Jones's Avatar
Catfish Jones Catfish Jones is offline
Paranoid User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: Alberta, Canada
Posts: 340 Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level)Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level)Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level)Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level)Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level)Catfish Jones User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 6 h 37 m 22 sec
Reputation Power: 32
So I was right? Um... wow... *blinks*

Reply With Quote
  #5  
Old June 15th, 2005, 04:52 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 18,246 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 17 h 3 m 28 sec
Reputation Power: 1055
yeah, the query in post #2 gives the average salary per department, but that's not what the original question was

the original question was to find the max average department salary, and say which department it's in

i know how to do it in every other database except oracle, which i flat out refuse to install (a 650meg download? you gots to be kidding me!)



besides, we don't do homework questions on these forums, do we

__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon

Reply With Quote
  #6  
Old June 15th, 2005, 06:52 PM
wordracr wordracr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 605 wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 18 m 28 sec
Reputation Power: 10
Just to make sure, the question was "find the max average department salary"? so you would just be returning 1 row , right?

If that's the case, break it down for yourself and pose the question, how do you get the average dept salaries listed?


select avg(salary), department_id, dept_name
from employees
group by department_id, dept_name;

(we are grouping by dept_name because it is one of the fields that you want to return.)

after that, you'd want to find the max of the results of that query(or rather, subquery) ^^.

Post your solution when you get it

Reply With Quote
  #7  
Old June 15th, 2005, 08:07 PM
rjett3345 rjett3345 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 21 rjett3345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 2 m 54 sec
Reputation Power: 0
its not a homework question, if it was a homework question id have the answer in my book. Im looking for the max average salary and its department number, so yes it would only return one row. I can get it to pull the avg sal and department id no problem, but if u try and nest the group by functions max(avg(salary)) then u get the not a single group group function. Its not a homework question or a test question, just something the students couldnt figure out so the teacher makes us thnk about it and try and find the answer. Anyone know how to do this?? please help if you know how to do this. ive been racking my brain all day over this question. Thanks

Do i need to do something to the department_id in the select statement (what i might need to do to it i dont know). Or do i need a having clause in there? the having clause wont let you nest group by functions so im at a loss completely... please someone help...

Reply With Quote
  #8  
Old June 15th, 2005, 10:13 PM
wordracr wordracr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 605 wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 18 m 28 sec
Reputation Power: 10
the answer is "secretly" in my post

Reply With Quote
  #9  
Old June 15th, 2005, 10:22 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 18,246 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 17 h 3 m 28 sec
Reputation Power: 1055
it's not all that secret, either, wordracr -- you actually come out and say it

Reply With Quote
  #10  
Old June 16th, 2005, 04:42 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 8,042 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 3 Weeks 6 Days 14 h 44 m 37 sec
Reputation Power: 281
Quote:
Originally Posted by r937
every other database except oracle, which i flat out refuse to install (a 650meg download? you gots to be kidding me!)

Too bad, it's great and who doesn't have a fast internet connection nowadays?
And btw ... I could ask you questions about analytic functions ...

Reply With Quote
  #11  
Old June 16th, 2005, 12:07 PM
hedge hedge is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2002
Posts: 693 hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 Days 38 m 9 sec
Reputation Power: 20
like this?
Code:
select deparment_id, max(salary)
 from (select department_id, avg(salary) salary
         from employees
       group by department_id)

Reply With Quote
  #12  
Old June 23rd, 2005, 01:40 PM
KK2796 KK2796 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 11 KK2796 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 40 sec
Reputation Power: 0
rjett3345,

The beginner way - break the problem into two easy questions:
1) What is the maximum of the average salaries for all departments?
2) Given the maximum average salary from #1, which department(s) has an average salary that is the same as #1?


- OR -

The intermediate way - follow wordarcs advice:
1) What are the average salaries for all departments?
2) Given that, can I somehow "pluck" the top element (think: ORDER BY and ROWNUM)

- OR -

The advanced way - impress the hell out of your professor, google for "analytic functions", spend an afternoon learning how the pros do it.

- OR -

Just keep posting your question to different discussion forums. Someone will answer it sooner or later.

Reply With Quote
  #13  
Old December 12th, 2005, 07:31 PM
merl merl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 51 merl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 6 m 34 sec
Reputation Power: 7
select *
from (select department_id, avg(salary) salary
from employees
group by department_id
order by avg(salary) desc)
where rownum = 1;

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > SQL group by problems HELP


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