每个部门的最高薪水

时间:2011-12-12 15:56:52

标签: sql sql-server tsql

我有一张表EmpDetails

DeptID      EmpName   Salary
Engg        Sam       1000
Engg        Smith     2000
HR          Denis     1500
HR          Danny     3000
IT          David     2000
IT          John      3000

我需要进行查询,找出每个部门的最高工资。

31 个答案:

答案 0 :(得分:29)

简短问题:

SELECT DeptID, MAX(Salary) FROM EmpDetails GROUP BY DeptID

答案 1 :(得分:21)

假设SQL Server 2005 +

WITH cteRowNum AS (
    SELECT DeptID, EmpName, Salary,
           DENSE_RANK() OVER(PARTITION BY DeptID ORDER BY Salary DESC) AS RowNum
        FROM EmpDetails
)
SELECT DeptID, EmpName, Salary
    FROM cteRowNum
    WHERE RowNum = 1;

答案 2 :(得分:5)

Select empname,empid,Sal,DeptName from 
(Select e.empname,e.empid,Max(S.Salary) Sal,D.DeptName, ROW_NUMBER() Over(partition by D.DeptName order by s.salary desc) Rownum
from emp e inner join Sal S
on e.empid=s.empid 
inner join Dept d on e.Deptid=d.Deptid
group by e.empname,e.empid,D.DeptName,s.Salary
) x where Rownum = 1

答案 3 :(得分:3)

使用以下问题:

select employee_name,salary,department_id from emp where salary in(select max(salary) from emp group by department_id);

答案 4 :(得分:3)

SELECT empName,empDept,EmpSalary
FROM Employee
WHERE empSalary IN
  (SELECT max(empSalary) AS salary
   From Employee
   GROUP BY EmpDept)

答案 5 :(得分:3)

如果您想要显示其他参数以及DeptIdSalary,例如EmpNameEmpId

SELECT 
        EmpID 
      , Name, 
      , Salary
      , DeptId 
   FROM Employee 
   where 
     (DeptId,Salary) 
     in 
     (select DeptId, max(salary) from Employee group by DeptId)

答案 6 :(得分:2)

这是ORACLE的最佳解决方案:

Select * from (select customerid, city, freight,
row_number() over (partition by customerid order by freight desc) Row_Number from 
(select orders.orderId, customers.CUSTOMERID, customers.city, orders.FREIGHT from orders inner join customers on orders.customerid = customers.customerid where customers.country='Germany' order by customers.customerid, orders.freight desc) 
order by customerid, freight desc) where Row_Number<=2;

注意这里我使用了partition by子句来标记行号,这主要是因为我们需要根据客户ID对记录进行分组。我在这里使用了两个内部查询。最内部的查询是给出根据客户ID和降低成本顺序排序的视图。现在我们需要获得前两个记录,所以首先我们需要命名它们然后我们需要根据rownum过滤它们。二级查询是根据客户ID标记rownum。最终查询将根据rownum过滤结果。对于每个分区。

答案 7 :(得分:2)

***

> /*highest salary by each dept*/

***
select d.Dept_Name,max(e.salary)
    from emp_details as e join Dept_Details as d
    on e.d_id=d.Dept_Id
    group by  d.Dept_Name

select  distinct e.d_id,d.Dept_Name
    from emp_details as e join Dept_Details as d 
    on e.d_id=d.Dept_Id

select  e.salary,d.Dept_Name,d.Dept_Id
    from emp_details as e join Dept_Details as d 
    on e.d_id=d.Dept_Id

/////simplest query for max salary dept_wise////

答案 8 :(得分:2)

SELECT D.DeptID, E.EmpName, E.Salary
FROM Employee E
INNER JOIN Department D ON D.DeptId = E.DeptId
WHERE E.Salary IN (SELECT MAX(Salary) FROM Employee);

答案 9 :(得分:2)

SELECT
    DeptID,
    Salary
FROM
    EmpDetails
GROUP BY
    DeptID
ORDER BY
    Salary desc

答案 10 :(得分:2)

使用以下命令;

SELECT  A.*
    FROM    @EmpDetails A
            INNER JOIN ( SELECT DeptID ,
                                MAX(salary) AS salary
                         FROM   @EmpDetails
                         GROUP BY DeptID
                       ) B ON A.DeptID = B.DeptID
                              AND A.salary = B.salary
    ORDER BY A.DeptID

答案 11 :(得分:2)

如果部门,工资和员工姓名在同一个表中,这将有效。

select ed.emp_name, ed.salary, ed.dept from
(select max(salary) maxSal, dept from emp_dept group by dept) maxsaldept
inner join emp_dept ed
on ed.dept = maxsaldept.dept and ed.salary = maxsaldept.maxSal

有没有比这更好的解决方案?

答案 12 :(得分:2)

SELECT Employee_ID
     , First_name
     , last_name
     , department_id
     , Salary 
FROM (SELECT Employee_ID
           , First_name
           , last_name
           , department_id
           , Salary
           , MAX(salary) OVER (PARTITION BY department_id) dept_max_sal
      FROM EMPLOYEES) AS Emp
WHERE salary = dept_max_sal;

答案 13 :(得分:2)

select a.*
 from EmpDetails a
 inner join 
 (
 select DeptID,max(Salary) as Salary
 from EmpDetails group by DeptID  
 )b 
on a.DeptID = b.DeptID and a.Salary = b.Salary

答案 14 :(得分:2)

WITH cteRowNum AS (
    SELECT DeptID, EmpName, Salary,
           ROW_NUMBER() OVER(PARTITION BY DeptID ORDER BY Salary DESC) AS RowNum
        FROM EmpDetails
)
SELECT DeptID, EmpName, Salary,Rownum
    FROM cteRowNum
    WHERE RowNum in(1,2);

答案 15 :(得分:2)

SELECT empname
FROM empdetails
WHERE salary IN(SELECT deptid max(salary) AS salary
FROM empdetails
group by deptid)

答案 16 :(得分:2)

是的,像是:

select 
   d.DeptID,
   max(e.Salary)
from
   department d
   inner join employees e on d.DeptID = e.DeptID
group by
  d.DeptID

答案 17 :(得分:1)

这是一种在任何版本的SQL上获取最大值和名称的方法。

测试数据:

CREATE TABLE EmpDetails(DeptID VARCHAR(10), EmpName VARCHAR(10), Salary DECIMAL(8,2))
INSERT INTO EmpDetails VALUES('Engg','Sam',1000)
INSERT INTO EmpDetails VALUES('Engg','Smith',2000)
INSERT INTO EmpDetails VALUES('HR','Denis',1500)
INSERT INTO EmpDetails VALUES('HR','Danny',3000)
INSERT INTO EmpDetails VALUES('IT','David',2000)
INSERT INTO EmpDetails VALUES('IT','John',3000)

示例:

SELECT ed.DeptID
      ,ed.EmpName
      ,ed.Salary
FROM (SELECT DeptID, MAX(Salary) MaxSal
      FROM EmpDetails
      GROUP BY DeptID)AS empmaxsal
INNER JOIN EmpDetails ed
  ON empmaxsal.DeptID = ed.DeptID
 AND empmaxsal.MaxSal = ed.Salary

不是最优雅,但它有效。

答案 18 :(得分:1)

SELECT DeptID, MAX(Salary)
 FROM EmpDetails
GROUP BY DeptID

此查询可以正常运行,但是如果您想要获取与薪水最高的员工相关的其他详细信息,则会发生矛盾。 您可以使用:

SELECT DepatID, a , b, c
 FROM EmpDetails
 WHERE Salary IN (
    SELECT max(Salary)
      FROM EmpDetails
     GROUP BY DeptID
 );

如果您将使用上一个查询,它将仅反映除工资之外的最小值的记录,因为您已使用max函数。

答案 19 :(得分:1)

如果您只想从该表中获得最高薪水,按部门:

SELECT MAX(Salary) FROM TableName GROUP BY DeptID

答案 20 :(得分:1)

select empno 
from EMP e 
where salary=(select max(sal) 
              from EMP w 
              where  groupby w.deptno having e.deptno=w.deptno)

我希望它能奏效......

答案 21 :(得分:1)

select deptid, empname, salary from
(Select deptid, empname,salary,
rank() Over(Partition by deptid  order by salary desc)as rank from 
EmpDetails) emp 
where emp.rank = 1

首先按薪酬按降序排列每位员工 等级1,然后只选择deptid,empname,salary。你可以这样做 所有第N组成员。

答案 22 :(得分:1)

使用相关子查询:

  <rewrite>
    <rules>
      <rule name="Imported Rule 1" stopProcessing="true">
        <match url="^(.*)/$" ignoreCase="false" />
        <conditions>
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
        </conditions>
        <action type="Redirect" redirectType="Permanent" url="/{R:1}" />
      </rule>
      <rule name="Imported Rule 2" stopProcessing="true">
        <match url="^" ignoreCase="false" />
        <conditions>
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
        </conditions>
        <action type="Rewrite" url="index.php" />
      </rule>
    </rules>
  </rewrite>

答案 23 :(得分:1)

以下查询将显示员工姓名及其各自的部门名称,其中该特定员工姓名的薪水最高。

with T as
(select empname, employee.deptno, salary
from employee
where salary in (select max(salary)
from employee
group by deptno))
select empname, deptname, salary
from T, department
where T.deptno=department.deptno;

我在Oracle数据库上成功执行了上述查询。

答案 24 :(得分:0)

我喜欢2种方法,其中一种方法使用 Rank ,另一种方法使用 ROW_NUMBER

这是我的示例数据

class Block: NSObject {
    let block: () -> Void
    init(block: @escaping () -> Void) {
        self.block = block
    }
}

class SomeClass: NSObject {
    @objc func foo() {
        print("foo...")
        let selectorName = "bar:"
        let selector = Selector(selectorName)
        if self.responds(to: selector) {
            let block = Block {
                print("block")
            }
            self.perform(selector, with: block)
        }
    }

    @objc func bar(_ arg1: Block) {
        print("bar...")
        arg1.block()
    }
}

这是我的第一个查询,用于查找最高薪水以及每个性别的具有最高薪水的人

Age          Name                                               Gender     Salary
----------- -------------------------------------------------- ---------- -----------
1           Mark                                               Male       5000
2           John                                               Male       4500
3           Pavan                                              Male       5000
4           Pam                                                Female     5500
5           Sara                                               Female     4000
6           Aradhya                                            Female     3500
7           Tom                                                Male       5500
8           Mary                                               Female     5000
9           Ben                                                Male       6500
10          Jodi                                               Female     7000
11          Tom                                                Male       5500
12          Ron                                                Male       5000
13          Ramani                                             Female     7000

因此,在这种情况下,我们可以看到这两名女员工“ Jodi”和“ Ramani”之间存在联系。在这种情况下,作为抢七局,我想将年龄作为决定因素,并且应该显示年龄更大的人

    with CTE as(
    select RANK() over(partition by Gender Order by Salary desc) as [Rank],* from employees)
    select * from CTE where [Rank]=1


Rank                 Age          Name                                               Gender     Salary
-------------------- ----------- -------------------------------------------------- ---------- -----------
1                    10          Jodi                                               Female     7000
1                    13          Ramani                                             Female     7000
1                    9           Ben                                                Male       6500

通常,在这种情况下,为了找到最高薪水,即使 使用Rank,Dense_Rank或Row_Number()。但是它们在其他情况下也会产生一些影响。

答案 25 :(得分:0)

with ctesal as (
 select DepartmentId , Name , Salary, ROW_Number() OVER (partition by DepartmentId 
 order by Salary desc) as RowNum
 from dbo.Employee
 )
 select DepartmentId , Name , Salary , RowNum from ctesal where RowNum  =2;

这适用于SQL Server。 ROW_Number是SQL Server中的内置函数,它基于partition by和order by子句给出从1开始的计数。最后,我们可以根据我们的要求写条件。

答案 26 :(得分:0)

下面列出的查询将列出每个部门的最高薪水。

export FLASK_DEBUG=1

我在Oracle数据库上成功执行了此查询。

答案 27 :(得分:0)

如果您想要部门和最高薪水,请使用

SELECT DeptID, MAX(Salary) FROM EmpDetails GROUP BY DeptID

如果您想在员工和部门中添加更多列,请使用

select  Department.Name , emp.Name, emp.Salary from Employee emp
inner join (select DeptID, max(salary) [salary] from employee group by DeptID) b
on emp.DeptID = b.DeptID and b.salary = emp.Salary
inner join Department on emp.DeptID = Department.id
order by Department.Name

如果像这样使用工资(选择最高(工资......)),一个人在另一个部门有相同的工资,那么它就会失败。

答案 28 :(得分:0)

select * from (
    select a.* from EmpDetails a 
    right join (select DeptID,max(salary) as Salary from EmpDetails group by DeptID) b
    on b.DeptID=a.DeptID and b.salary=a.salary ) as c  group by c.DeptID;

答案 29 :(得分:-1)

从中选择T1。* (选择empname作为e1,department_name,雇员的薪水)作为T1, (按部门名称从员工组中选择max(salary)maxsal,部门名称dept1)作为T2 其中T1.department_name = T2.dept1和T1.salary = maxsal;

答案 30 :(得分:-1)

不确定为什么没有人提到Group By ....有语法。 它专门解决了这些要求。

select EmpName,DeptId,Salary from EmpDetails group by DeptId having Salary=max(Salary);