2016-03-17 73 views
0

I創建表EmployeeDepartmentEmployee表包含工資和部門ID作爲外鍵的員工詳細信息。列表部門和平均工資以升序排列

我想顯示列表部門和平均工資按升序排列。

department表包含deptid和deptnm。

DEPTID(pk) DEPTNM  DHEAD 
------------------------ 
1   COMPUTER  PATIL SIR 
2   MARATHI  PAWAR SIR 
3   MICRO   JADHAV SIR 
4   BIOTECH  BHOSALE SIR 
5   BOTANY  JADHAV SIR 

EMPID(pk) LASTNAME FIRSTNAME MIDDLENAME DOB  DOJ SALARY EMPDEPTID 
---------------------------------------------------------------------------- 
1  PAWAR SIDDHANT SANGHA  29-APR-97 02-JUL-15 10000  1 
2  ADKE  SHUBHAM GIRISH  02-AUG-96 03-AUG-15 9000  2 
3  CHOUGULE NEERAJ SUBHASH 01-MAR-94 05-DEC-15 7000  3 
4  MOTE  AJAY  SHAHAJI 05-JUN-96 10-NOV-15 4500  4 
5  SAHASTRA ASHWIN MADAN  21-NOV-96 21-NOV-15 9000  5 
+0

如果答案的人幫助你,選擇它作爲公認的答案,讓其他人知道這個問題是回答。 –

回答

0

然後,您需要按部門分組並計算平均工資,然後按其排序。萬一Department.DEPTNM不是唯一的,我們組由DEPTID

SELECT Employee.EMPDEPTID, AVG(Employee.SALARY) as AVERAGE_SALARY 
FROM Employee 
GROUP BY Department.DEPTID 
ORDER BY AVG(Employee.SALARY) 

如果你需要得到主管部門的名稱,那麼你可以後加入到Department

SELECT * FROM ( 

SELECT Employee.EMPDEPTID, AVG(Employee.SALARY) as AVERAGE_SALARY 
FROM Employee 
GROUP BY Department.DEPTID 
ORDER BY AVG(Employee.SALARY) 

) as AVG_SALARY_PER_DEPT 

JOIN Department ON (Department.DEPTID = AVG_SALARY_PER_DEPT.EMPLDEPTID) 
+0

謝謝你的朋友... –

0

您需要:

你的情況:

SELECT AVG(SALARY), DEPTID, DEPTNM, DHEAD 
    FROM department 
     INNER JOIN employee 
     ON (DEPTID = EMPDEPTID) 
GROUP BY DEPTID, DEPTNM, DHEAD 
ORDER BY 1 
+0

謝謝朋友... –