MySQL查询在单独的行中使用GROUP BY和多个表连接获取max,min

时间:2014-07-31 20:04:53

标签: mysql sql

表格结构

employee_salary

salary_id  |    emp_id    |   salary

员工表格结构

emp_id | first_name | last_name | gender | email | mobile | dept_id | is_active

部门:

dept_id | dept_name | manager_name | is_active

问题:

显示获得最高和最低工资额的部门明智员工?

我正在使用查询

SELECT   max(salary) salary,  dept_name, first_name, dept_id , 'MAX' Type
FROM    ( 
        SELECT     a.salary, c.dept_name, b.first_name, b.dept_id, a.salary_id
        FROM       employee_salary a
        LEFT JOIN  employee b 
        ON         a.emp_id = b.emp_id
        LEFT JOIN  department c 
        ON c.dept_id = b.dept_id
        ) t
GROUP BY dept_id

UNION ALL

SELECT   min(salary) salary,  dept_name, first_name, dept_id , 'MIN' Type
FROM    ( 
        SELECT     a.salary, c.dept_name, b.first_name, b.dept_id, a.salary_id
        FROM       employee_salary a
        LEFT JOIN  employee b 
        ON         a.emp_id = b.emp_id
        LEFT JOIN  department c 
        ON c.dept_id = b.dept_id
        ) t
GROUP BY dept_id

ORDER BY dept_id 

我得到的输出如下所示。 我无法从员工表中获取相应的名字,而其他所有字段都显示正确的值

salary  dept_name   first_name  dept_id Type

30000   dept_1      Paul        1       MIN
98000   dept_1      Paul        1       MAX
51000   dept_2      Aron        2       MAX
20000   dept_2      Aron        2       MIN
40000   dept_3      Steve       3       MAX
40000   dept_3      Steve       3       MIN
64000   dept_4      Henry       4       MAX
64000   dept_4      Henry       4       MIN

4 个答案:

答案 0 :(得分:1)

您的选择列表中不能包含非聚合函数或包含在您的组中的值。确定employeeid具有最高或最低工资后,选择名称。

答案 1 :(得分:0)

您的问题是,您只想在一个查询中获取聚合(GROUP BY)结果和详细结果(员工姓名)。你不能按照你想要的方式做到这一点。

让我们分解吧。您需要找到每个部门的最低工资。你喜欢这样:

     SELECT  MIN(a.salary) salary, b.dept_id
       FROM  employee_salary a
  LEFT JOIN  employee b   ON  a.emp_id = b.emp_id
  LEFT JOIN  department c ON c.dept_id = b.dept_id
   GROUP BY  b.dept_id

这会为您提供最低工资清单。接下来,您必须找到每个部门中哪个员工具有该最低薪水:因此,您必须将此查询加入另一个员工。看起来像这样:

     SELECT a.salary, c.dept_name, b.first_name, b.dept_id
       FROM employee_salary a
  LEFT JOIN employee b ON  a.emp_id = b.emp_id
  LEFT JOIN department c ON c.dept_id = b.dept_id
       JOIN ( /* the above aggregate query */
               SELECT  MIN(a.salary) salary, b.dept_id
                 FROM  employee_salary a
            LEFT JOIN  employee b   ON  a.emp_id = b.emp_id
            LEFT JOIN  department c ON c.dept_id = b.dept_id
             GROUP BY  b.dept_id
            ) d ON d.salary=a.salary AND d.dept_id = b.dept_id

这将为您提供与每个部门中具有最低工资的员工(或员工)相对应的员工/部门/工资明细数据。

您可以采取同样的措施让员工获得最高工资。

答案 2 :(得分:0)

不漂亮,但似乎有效。制作了最低和最高工资的派生表,并将其与其他表格相结合。

<强> SQL Fiddle Demo

select es.salary,
  ds.dept_name,
  e.first_name,
  ds.dept_id,
  'MAX' Type
from employee_salary es
join employee e on es.emp_id = e.emp_id
join
  (SELECT     b.dept_id,
              c.dept_name, 
             max(salary) as 'MaxSalary' 
   FROM       employee_salary a
   LEFT JOIN  employee b 
     ON         a.emp_id = b.emp_id
   LEFT JOIN  department c 
     ON c.dept_id = b.dept_id
   GROUP BY b.dept_id, c.dept_name) ds on e.dept_id = ds.dept_id and es.salary = ds.maxsalary

UNION ALL

select es.salary,
  ds.dept_name,
  e.first_name,
  ds.dept_id,
  'MIN' Type
from employee_salary es
join employee e on es.emp_id = e.emp_id
join
  (SELECT     b.dept_id,
              c.dept_name, 
           min(salary) as 'MinSalary'
   FROM       employee_salary a
   LEFT JOIN  employee b 
    ON         a.emp_id = b.emp_id
   LEFT JOIN  department c 
    ON c.dept_id = b.dept_id
   GROUP BY b.dept_id, c.dept_name) ds on e.dept_id = ds.dept_id and es.salary = ds.minsalary

ORDER BY dept_id, salary

答案 3 :(得分:0)

将工资与DESC订购以查找最大值,并通过ASC查找最低工资 这是因为它检查了第一个匹配的术语,并相应地返回值,

   SELECT max(salary) salary,  dept_name, first_name, dept_id , 'MAX' Type
    FROM ( SELECT a.salary, c.dept_name, b.first_name, b.dept_id, a.salary_id
    FROM employee_salary a
    LEFT JOIN employee b ON a.emp_id = b.emp_id
    LEFT JOIN department c ON c.dept_id = b.dept_id ORDER BY a.salary DESC) t
    group by dept_id
    UNION ALL
    SELECT min(salary) salary,  dept_name, first_name, dept_id , 'MIN' Type
    FROM ( SELECT a.salary, c.dept_name, b.first_name, b.dept_id, a.salary_id
    FROM employee_salary a
    LEFT JOIN employee b ON a.emp_id = b.emp_id
    LEFT JOIN department c ON c.dept_id = b.dept_id  ORDER BY a.salary ASC) t
    group by dept_id
    ORDER BY dept_id