create table #tempEmp
(
Salary decimal(18,2)
)
insert into #tempEmp(Salary) values(1000),(500),(200),(400),(3000),(2000),(1500),(5000),(1000),(500)
select Salary,count(Salary) as count from #tempEmp group by #tempEmp.salary order by 1 desc
second highest salary:-
select t.Salary as Salary from #tempEmp t where 2 =(select count(distinct t1.salary) from #tempEmp t1 where t1.Salary >= t.Salary)
output: 3000
third highest salary:-
select t.Salary as Salary from #tempEmp t where 3 =(select count(distinct t1.salary) from #tempEmp t1 where t1.Salary >= t.Salary)
output: 2000
Fifth highest salary:-
select t.Salary as Salary from #tempEmp t where 5 =(select count(distinct t1.salary) from #tempEmp t1 where t1.Salary >= t.Salary)
output: 1000
1000
so it gives two times 1000 but desired output should be only one 1000.
So modified above query by using Max(Salary)
select Max(t.Salary) as Salary from #tempEmp t where 5 =(select count(distinct t1.salary) from #tempEmp t1 where t1.Salary >= t.Salary)
(
Salary decimal(18,2)
)
insert into #tempEmp(Salary) values(1000),(500),(200),(400),(3000),(2000),(1500),(5000),(1000),(500)
select Salary,count(Salary) as count from #tempEmp group by #tempEmp.salary order by 1 desc
second highest salary:-
select t.Salary as Salary from #tempEmp t where 2 =(select count(distinct t1.salary) from #tempEmp t1 where t1.Salary >= t.Salary)
output: 3000
third highest salary:-
select t.Salary as Salary from #tempEmp t where 3 =(select count(distinct t1.salary) from #tempEmp t1 where t1.Salary >= t.Salary)
output: 2000
Fifth highest salary:-
select t.Salary as Salary from #tempEmp t where 5 =(select count(distinct t1.salary) from #tempEmp t1 where t1.Salary >= t.Salary)
output: 1000
1000
so it gives two times 1000 but desired output should be only one 1000.
So modified above query by using Max(Salary)
select Max(t.Salary) as Salary from #tempEmp t where 5 =(select count(distinct t1.salary) from #tempEmp t1 where t1.Salary >= t.Salary)
No comments:
Post a Comment