Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Probelms with AVG statement and GROUP BY clause

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-10-31 : 18:53:43
J-Ron writes "Hi,
Im trying to write a qeury that lists all the employess of a company by department, and show the average salary for each department. Sounds simple enough but i cant get it to work. What ive got right now is:

SELECT DeptName, AVG(HourlyRate) AS Average

FROM A6_Employees INNER JOIN A6_Departments ON
A6_Employees.DeptNo = A6_Departments.DeptNo
GROUP BY DeptName


But like i said i need to list the employees as well. I cant seem to get this to work without changing the grouping, which then causes my AVG statement to calculate the average HourlyRate per employee, not department. Any help would be much appreciated"

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-10-31 : 20:45:26
SELECT e.EmpName, dt.DeptName, dt.Average FROM A6_Employees e INNER JOIN (
SELECT DeptName, AVG(HourlyRate) AS Average

FROM A6_Employees INNER JOIN A6_Departments ON
A6_Employees.DeptNo = A6_Departments.DeptNo
GROUP BY DeptName) dt ON e.DeptNo = dt.DeptNo

This is a guess of course.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -