如何获得第二高的账户部门薪酬?

时间:2022-12-12 13:12:34

I having three tables like below -:

我有三张桌子如下 - :

Create table #temp (id int, DepartmentName varchar(50))
insert into #temp (id,DepartmentName) values(1,'Account')
insert into #temp (id,DepartmentName) values(2,'IT')
select * from #temp

Create Table #temp1(customerid int, CustomerName varchar(50),DepartmentId int)
Insert into #temp1(customerid,CustomerName,DepartmentId)values(1,'Anil',1)
Insert into #temp1(customerid,CustomerName,DepartmentId)values(2,'Ankit',2)
Insert into #temp1(customerid,CustomerName,DepartmentId)values(3,'Mandeep',1)
Insert into #temp1(customerid,CustomerName,DepartmentId)values(4,'Rajesh',2)
Insert into #temp1(customerid,CustomerName,DepartmentId)values(5,'Rohit',1)
Insert into #temp1(customerid,CustomerName,DepartmentId)values(6,'Sharma',0)

Create Table #temp2(customerid int, salary int)
insert into #temp2(customerid,salary)values(1,2000)
insert into #temp2(customerid,salary)values(3,2399)
insert into #temp2(customerid,salary)values(4,4000)
insert into #temp2(customerid,salary)values(2,4500)
insert into #temp2(customerid,salary)values(5,7000)


select max(t2.salary) ,t.CustomerName,t1.DepartmentName
from #temp1 t
left join #temp t1 on t1.id=t.DepartmentId
left join #temp2 t2 on t2.customerid=t.customerid
where DepartmentName='Account' 
and salary<>(select max(tt2.salary)  from #temp2 tt2 
inner join #temp1 tt1 on tt1.customerid=tt2.customerid 
inner join #temp tt on tt.id=tt1.DepartmentId
where tt.DepartmentName='Account') 
group by CustomerName,DepartmentName

But not getting second highest salary with this, could please anybody help me with this, i am using max aggregate function in outer query but still getting all the salary of account department.

但是没有得到第二高的薪水,可以请任何人帮我这个,我在外部查询中使用max聚合函数但仍然获得帐户部门的所有工资。

6 个解决方案

#1


3  

Use Dense_Rank

SELECT
     DepartmentName,
     CustomerName,
     salary
FROM
    (
    SELECT
        t.DepartmentName,
        DENSE_RANK() OVER( PARTITION BY id ORDER BY salary desc) rno,
        salary,
        t1.CustomerName
    FROM #temp t
    JOIN #temp1 t1
        ON t.id = t1.DepartmentId
    JOIN #temp2 t2
        ON t1.customerid = t2.customerid 
    where  t.DepartmentName='Account'
    ) a

WHERE rno = 2 

Update Modified Your Query with dense_rank since we can not perform group by with customer names.

更新使用dense_rank修改您的查询,因为我们无法使用客户名称执行分组。

SELECT salary,
       CustomerName,
       DepartmentName
FROM   (SELECT t2.salary,
               t.CustomerName,
               t1.DepartmentName,
               Dense_rank()
                 OVER(
                   partition BY DepartmentName
                   ORDER BY salary DESC) rno
        FROM   #temp1 t
               LEFT JOIN #temp t1
                      ON t1.id = t.DepartmentId
               LEFT JOIN #temp2 t2
                      ON t2.customerid = t.customerid
        WHERE  DepartmentName = 'Account'
               AND salary NOT IN (SELECT Max(tt2.salary)
                                  FROM   #temp2 tt2
                                         INNER JOIN #temp1 tt1
                                                 ON tt1.customerid = tt2.customerid
                                         INNER JOIN #temp tt
                                                 ON tt.id = tt1.DepartmentId
                                  WHERE  tt.DepartmentName = 'Account'))a
WHERE  rno = 1 

#2


1  

Try This :

试试这个 :

 SELECT #temp1.customerid,
       CustomerName,
       DepartmentName,
       Max(salary)
FROM   #temp
       JOIN #temp1
         ON id = DepartmentId
       JOIN #temp2
         ON #temp1.customerid = #temp2.customerid
WHERE  id = 1
       AND salary NOT IN (SELECT Max(salary)
                          FROM   #temp2)
GROUP  BY #temp1.customerid,
          CustomerName,
          DepartmentName 

#3


1  

This is the easiest and fastest way of doing it. Better than using rank...

这是最简单,最快速的方法。比使用排名更好......

select top 1
    t1.Customerid,
    t1.CustomerName,
    t2.Salary,
    t.DepartmentName

from #temp1 t1

Join #temp2 t2
    on t2.customerId = t1.Customerid

Join #temp t
    on t.id = t1.departmentid
    and t.departmentName = 'account'

where t2.salary <
    (
    select MAX(t2.salary)
    from #temp1 t1
    Join #temp2 t2
        on t2.customerId = t1.Customerid
    Join #temp t
        on t.id = t1.departmentid
        and t.departmentName = 'account'
    )

order by t2.salary desc

#4


0  

;WITH cte AS(

SELECT t1.customerid,t1.CustomerName,t2.DepartmentName FROM #temp1 t1 
                                        LEFT JOIN #temp t2 ON t1.DepartmentId = t2.id)

,cte2 as(
SELECT t3.*,t4.salary,ROW_NUMBER() OVER(ORDER BY t4.salary DESC) AS rno FROM cte t3
                                        LEFT JOIN #temp2 t4 ON t3.customerid = t4.customerid)
SELECT * FROM cte2 WHERE rno=2

#5


0  

I also try :)

我也试试:)

with cte as 
(
    select
        c.CustomerName,
        d.DepartmentName,
        s.Salary,
        RowNumber = row_number() over (partition by c.DepartmentId order by s.Salary)
    from 
        #temp1 c
        inner join #temp d on d.Id = c.DepartmentId
        inner join #temp2 s on s.CustomerId = c.CustomerId
)
select
    CustomerName,
    DepartmentName,
    Salary
from
    cte
where
    RowNumber = 2
    and
    DepartmentName='Account';

Edited: dance_rank is better here. Buddi gave his answer earlier!

编辑:dance_rank在这里更好。 Buddi早些时候回答了他的问题!

#6


-2  

select t1.customerid,t2.salary,t1.CustomerName,t.DepartmentName 
from #temp2 t2 
 join  #temp1  t1
on t2.customerid=t1.customerid
 join #temp t
on t1.DepartmentId =t.id
where salary=(
select MIN(salary) as salary from (select distinct top(2) salary from #temp2 order by salary desc )T)

Try above code

试试上面的代码

#1


3  

Use Dense_Rank

SELECT
     DepartmentName,
     CustomerName,
     salary
FROM
    (
    SELECT
        t.DepartmentName,
        DENSE_RANK() OVER( PARTITION BY id ORDER BY salary desc) rno,
        salary,
        t1.CustomerName
    FROM #temp t
    JOIN #temp1 t1
        ON t.id = t1.DepartmentId
    JOIN #temp2 t2
        ON t1.customerid = t2.customerid 
    where  t.DepartmentName='Account'
    ) a

WHERE rno = 2 

Update Modified Your Query with dense_rank since we can not perform group by with customer names.

更新使用dense_rank修改您的查询,因为我们无法使用客户名称执行分组。

SELECT salary,
       CustomerName,
       DepartmentName
FROM   (SELECT t2.salary,
               t.CustomerName,
               t1.DepartmentName,
               Dense_rank()
                 OVER(
                   partition BY DepartmentName
                   ORDER BY salary DESC) rno
        FROM   #temp1 t
               LEFT JOIN #temp t1
                      ON t1.id = t.DepartmentId
               LEFT JOIN #temp2 t2
                      ON t2.customerid = t.customerid
        WHERE  DepartmentName = 'Account'
               AND salary NOT IN (SELECT Max(tt2.salary)
                                  FROM   #temp2 tt2
                                         INNER JOIN #temp1 tt1
                                                 ON tt1.customerid = tt2.customerid
                                         INNER JOIN #temp tt
                                                 ON tt.id = tt1.DepartmentId
                                  WHERE  tt.DepartmentName = 'Account'))a
WHERE  rno = 1 

#2


1  

Try This :

试试这个 :

 SELECT #temp1.customerid,
       CustomerName,
       DepartmentName,
       Max(salary)
FROM   #temp
       JOIN #temp1
         ON id = DepartmentId
       JOIN #temp2
         ON #temp1.customerid = #temp2.customerid
WHERE  id = 1
       AND salary NOT IN (SELECT Max(salary)
                          FROM   #temp2)
GROUP  BY #temp1.customerid,
          CustomerName,
          DepartmentName 

#3


1  

This is the easiest and fastest way of doing it. Better than using rank...

这是最简单,最快速的方法。比使用排名更好......

select top 1
    t1.Customerid,
    t1.CustomerName,
    t2.Salary,
    t.DepartmentName

from #temp1 t1

Join #temp2 t2
    on t2.customerId = t1.Customerid

Join #temp t
    on t.id = t1.departmentid
    and t.departmentName = 'account'

where t2.salary <
    (
    select MAX(t2.salary)
    from #temp1 t1
    Join #temp2 t2
        on t2.customerId = t1.Customerid
    Join #temp t
        on t.id = t1.departmentid
        and t.departmentName = 'account'
    )

order by t2.salary desc

#4


0  

;WITH cte AS(

SELECT t1.customerid,t1.CustomerName,t2.DepartmentName FROM #temp1 t1 
                                        LEFT JOIN #temp t2 ON t1.DepartmentId = t2.id)

,cte2 as(
SELECT t3.*,t4.salary,ROW_NUMBER() OVER(ORDER BY t4.salary DESC) AS rno FROM cte t3
                                        LEFT JOIN #temp2 t4 ON t3.customerid = t4.customerid)
SELECT * FROM cte2 WHERE rno=2

#5


0  

I also try :)

我也试试:)

with cte as 
(
    select
        c.CustomerName,
        d.DepartmentName,
        s.Salary,
        RowNumber = row_number() over (partition by c.DepartmentId order by s.Salary)
    from 
        #temp1 c
        inner join #temp d on d.Id = c.DepartmentId
        inner join #temp2 s on s.CustomerId = c.CustomerId
)
select
    CustomerName,
    DepartmentName,
    Salary
from
    cte
where
    RowNumber = 2
    and
    DepartmentName='Account';

Edited: dance_rank is better here. Buddi gave his answer earlier!

编辑:dance_rank在这里更好。 Buddi早些时候回答了他的问题!

#6


-2  

select t1.customerid,t2.salary,t1.CustomerName,t.DepartmentName 
from #temp2 t2 
 join  #temp1  t1
on t2.customerid=t1.customerid
 join #temp t
on t1.DepartmentId =t.id
where salary=(
select MIN(salary) as salary from (select distinct top(2) salary from #temp2 order by salary desc )T)

Try above code

试试上面的代码