Sql命令使用以下四个表的连接

时间:2010-01-25 12:09:33

标签: sql

有四个表

  1. 员工(Employee_id,name,chief_id)
  2. 署(DEPARTMENT_ID,名称)
  3. Emp_Dep(EMPLOYEE_ID,DEPARTMENT_ID)
  4. Emp_Sal(EMPLOYEE_ID,薪水)
  5. 现在我需要编写一个查询,显示每个部门获得最高薪水的员工ID。

5 个答案:

答案 0 :(得分:0)

您没有说明您正在使用哪种数据库(除非您打算 SQL = SQL-Server )。所以这个解决方案不适合你。任何,它使用Oracle的分析函数RANK()来解决问题。

鉴于此数据......

SQL> select d.name
  2         , e.name
  3         , es.salary
  4  from employee e join
  5       emp_sal es on (es.employee_id = e.employee_id) join
  6       emp_dep ed on (ed.employee_id = e.employee_id) join
  7       department d on (d.department_id = ed.department_id)
  8  order by d.department_id, es.salary desc
  9  /

NAME       NAME           SALARY
---------- ---------- ----------
PRODUCTION GRINCH           1100
PRODUCTION CAT              1100
DELIVERY   LORAX            3000
DELIVERY   DAISY            2500
ACCOUNTS   FOX              5000
ACCOUNTS   KNOX             2500
ACCOUNTS   SAM-I-AM         1200

7 rows selected.

SQL>

注意PRODUCTION中的关系,这就是为什么这会返回四行......

SQL> select dept_name
  2         , emp_name
  3         , salary
  4  from (
  5          select d.name as dept_name
  6                 , e.name as emp_name
  7                 , es.salary
  8                 , rank () over (partition by d.department_id
  9                                    order by es.salary desc ) as rnk
 10          from employee e join
 11               emp_sal es on (es.employee_id = e.employee_id) join
 12               emp_dep ed on (ed.employee_id = e.employee_id) join
 13               department d on (d.department_id = ed.department_id)
 14         )
 15  where rnk = 1
 16  order by salary desc
 17  /


DEPT_NAME  EMP_NAME       SALARY
---------- ---------- ----------
ACCOUNTS   FOX              5000
DELIVERY   LORAX            3000
PRODUCTION GRINCH           1100
PRODUCTION CAT              1100

SQL>

修改

我使用员工姓名而不是ID,因为它使结果更容易阅读。您可以轻松替换所需的投影仪。

答案 1 :(得分:0)

这将为您提供最高工资的价值:

SELECT 
  department_id, MAX(es.salary) AS max_salary
FROM 
  emp_sal es JOIN emp_dep ed ON ed.employee_id = es.employee_id
GROUP BY ed.department_id

这将为您提供ID:

SELECT department_id, employee_id, max_values.max_salary
FROM 
  emp_sal es JOIN emp_dep ed ON ed.employee_id = es.employee_id
  JOIN 
    (SELECT 
       department_id, MAX(es.salary) AS max_salary
     FROM 
       emp_sal es JOIN emp_dep ed ON ed.employee_id = es.employee_id
     GROUP BY ed.department_id) AS max_values 
  ON (ed.department_id = max_values.department_id AND es.salary = max_salary)

答案 2 :(得分:-1)

我认为以下内容应该有效 [EDITED]

SELECT 
    ed.department_id,
    d.[name],
    es.employee_id,
    e.[name],
    es.salary
FROM  
    emp_sal es
    INNER JOIN emp_dep ed
        ON ed.employee_id = es.employee_id
    INNER JOIN employee e
        ON e.employee_id = ed.employee_id
    INNER JOIN department d
        ON d.department_id = ed.department_id
WHERE 
  es.salary =   (
                    SELECT 
                        max(iEs.salary)
                    FROM
                        emp_sal iEs
                        INNER JOIN emp_dep iEd
                        ON iEd.employee_id = iEs.employee_id
                    WHERE 
                        iEd.department_id = ed.department_id
                )

为了显示员工和部门的名称,我做得有点复杂。

仅供员工ID使用

SELECT 
    es.employee_id
FROM  
    emp_sal es
    INNER JOIN emp_dep ed
        ON ed.employee_id = es.employee_id
WHERE 
  es.salary =   (
                    SELECT 
                        max(iEs.salary)
                    FROM
                        emp_sal iEs
                        INNER JOIN emp_dep iEd
                        ON iEd.employee_id = iEs.employee_id
                    WHERE 
                        iEd.department_id = ed.department_id
                )

答案 3 :(得分:-1)

创建我自己的测试表:

DECLARE @Employee TABLE (Employee_id INT, Name VARCHAR(MAX), Chief_id INT)
DECLARE @Department TABLE (Department_Id INT, Name VARCHAR(MAX))
DECLARE @Emp_Dep TABLE (Employee_id INT, Department_Id INT)
DECLARE @Emp_Sal TABLE (Employee_id INT, Salary DECIMAL)

INSERT INTO @Employee
SELECT  1, 'John Doe', 0 UNION ALL
SELECT  2, 'John Doe', 0 UNION ALL
SELECT  3, 'John Doe', 0 UNION ALL
SELECT  4, 'John Doe', 0 UNION ALL
SELECT  5, 'John Doe', 0 UNION ALL
SELECT  6, 'John Doe', 0 UNION ALL
SELECT  7, 'John Doe', 0;

INSERT INTO @Department
SELECT 1, 'Moomin' UNION ALL
SELECT 2, 'Moo' UNION ALL
SELECT 3, 'Min';

INSERT INTO @Emp_Dep
SELECT 1, 1 UNION ALL
SELECT 2, 1 UNION ALL
SELECT 3, 2 UNION ALL
SELECT 4, 2 UNION ALL
SELECT 5, 3 UNION ALL
SELECT 6, 3 UNION ALL
SELECT 7, 3;

INSERT INTO @Emp_Sal
SELECT 1, 1200 UNION ALL
SELECT 2, 1100 UNION ALL
SELECT 3, 2000 UNION ALL
SELECT 4, 2000 UNION ALL
SELECT 5, 3451 UNION ALL
SELECT 6, 3321 UNION ALL
SELECT 7, 3123;

如果他们被捆绑在一起,那么这个版本将是部门中的所有人:)

WITH [Merged] AS (
    SELECT [@Employee].[Employee_id],
           [@Employee].[Name],
           [@Department].[Name] [Department],
           [@Department].[Department_Id],
           [@Emp_Sal].[Salary]
      FROM @Employee
     INNER JOIN @Emp_Sal ON [@Employee].[Employee_id] = [@Emp_Sal].[Employee_id]
     INNER JOIN @Emp_Dep ON [@Emp_Sal].[Employee_id] = [@Emp_Dep].[Employee_id]
     INNER JOIN @Department ON [@Emp_Dep].[Department_Id] = [@Department].[Department_Id]
)
SELECT [Main].Employee_id, [Main].Name, [Main].Department, [Main].Salary
FROM Merged [Main]
WHERE [Salary] = (SELECT MAX(Salary) FROM [Merged] WHERE Department_Id = [Main].Department_Id);

虽然这个版本只会从每个部门中选择1,但你必须找到一种方法让你想要的人在ROW_NUMBER()OVER

之上。
WITH [Merged] AS (
    SELECT [@Employee].[Employee_id],
           [@Employee].[Name],
           [@Department].[Name] [Department],
           [@Department].[Department_Id],
           [@Emp_Sal].[Salary],
           ROW_NUMBER() OVER(ORDER BY [@Department].[Department_Id] ASC, [@Emp_Sal].[Salary] DESC, [@Employee].[Employee_id] ASC) AS 'RowNumber'
      FROM @Employee
     INNER JOIN @Emp_Sal ON [@Employee].[Employee_id] = [@Emp_Sal].[Employee_id]
     INNER JOIN @Emp_Dep ON [@Emp_Sal].[Employee_id] = [@Emp_Dep].[Employee_id]
     INNER JOIN @Department ON [@Emp_Dep].[Department_Id] = [@Department].[Department_Id]
)
SELECT [Main].Employee_id, [Main].Name, [Main].Department, [Main].Salary
FROM Merged [Main]
WHERE [RowNumber] = (SELECT MAX(RowNumber) FROM [Merged] WHERE Department_Id = [Main].Department_Id);

值得一提的可能是它适用于Microsoft SQL Server!

答案 4 :(得分:-2)

select
    e.employee_id
    d.name as department,
    max(s.salary) as msalary
from
    department as d       
right join emp_dep as e on e.department_id = d.department_id
left join emp_sal as s on s.employee_id = e.employee_id
group by d.name

或类似的东西