2011-10-12 120 views
0

这里的号码是我的代码:SQL查询,显示部门名称,地点名称,员工

SELECT dname,loc,avg(sal) 
FROM dept,emp 
GROUP BY loc; 

我想写一个查询,显示该部门名称,位置,以及员工人数和平均工资为该部门的所有员工。标记列dname,loc,员工数量和Avgsalary。

的数据是:在你的选择列表,并通过你的小组

DNAME   LOC   Number of People  Salary       
-------------- ------------- ---------------- ----------       
SALES   CHICAGO      6 1566.67       
RESEARCH  DALLAS      5  2175       
ACCOUNTING  NEW YORK      3 2916.67    

回答

0

你只是错过了SUM(1)可能是错误的,这个问题:

select dname as DNAME, loc as LOC, SUM(1) as Number_of_People, avg(sal) as AvgSalary from dept,emp group by dname, loc; 
1

--No需要SUM(1)使用COUNT(e.deptno)人数 -

select d.dname as DNAME, d.loc as LOC, count(e.deptno)as "Number of people", round(avg(e.sal),2) as "Salary" from dept d, emp e where d.deptno = e.deptno group by d.dname, d.loc, e.deptno; 
相关问题