尋找部門薪資的平均水平
P粉329425839
2023-09-02 20:56:48
<p>我有兩個表</p>
<pre class="brush:php;toolbar:false;">db_employee (id,first_name,last_name,salary,department_id)
db_dept (department_id,department)</pre>
<pre class="brush:php;toolbar:false;">這裡有一些範例數據
db_employee
id - fist_name - last_name - salary - department_id
10301 - Keith - Morgan - 27056 - 2
10302 - Tyler - Booth - 32199 - 3
db_dept
id - department
2 - 人力資源
3 - 營運</pre>
<p>我想要輸出一個表格,顯示員工、他們的薪水以及該員工所在部門的平均薪水。 </p>
<p>我嘗試先使用子查詢找到部門的平均薪水,然後再進行外部查詢,但是我遇到了一個錯誤。 </p>
<pre class="brush:php;toolbar:false;">Select
first_name,
last_name,
salary,
(
select
avg(emp.salary),
dep.department
from db_employee emp
join db_dept dep on emp.department_id=dep.id
group by dep.department
) As avgsaldepartment
from db_employee</pre></p>