對於架構epartments(department_id, department_name) employees(last_name, department_id, salary)
我想要顯示的department_id, department_name, count(employees),avg(salary),last_name,salary
我曾嘗試使用下面的查詢如何顯示部門(一次)以及在該部門工作的所有員工的列表?
SELECT d1.department_id,d1.department_name,d1."count",d1."avg",e.last_name,e.salary
FROM employees e
INNER JOIN (SELECT d.department_id,d.department_name,count(e.last_name) AS "count",round(avg(e.salary),2) AS "avg"
FROM employees e,departments d
WHERE e.department_id=d.department_id
GROUP BY d.department_id,d.department_name) d1
ON e.department_id=d1.department_id;
雖然顯示正確的輸出它不是我想要的格式。 上面的查詢給出的
90 Executive 3 19333.33 King 24000
90 Executive 3 19333.33 Kochhar 17000
90 Executive 3 19333.33 De Haan 17000
60 IT 3 6400 Hunold 9000
60 IT 3 6400 Ernst 6000
60 IT 3 6400 Lorentz 4200
50 Shipping 5 3500 Mourgos 5800
輸出,而應該像
90 Executive 3 19333.33 King 24000
Kochhar 17000
De Haan 17000
60 IT 3 6400 Hunold 9000
Ernst 6000
Lorentz 4200
50 Shipping 5 3500 Mourgos 5800
而不是在SQL中這樣做,這樣做表示層代碼的格式 – HaveNoDisplayName