Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select query to fetch particular name at the top 5

Author  Topic 

dhinasql
Posting Yak Master

195 Posts

Posted - 2009-04-23 : 02:20:50
i need to fetch top 5 value from the table where my required name should be top

for ex:
select top 5 salary from employee order by case when
name='abc' then 0 else 1 end

this shows if name='abc' is in top.it will select either it will not in top 5


But what i need is, always name 'abc' should be in top. salary is not in mandatory







SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-23 : 02:32:31
select top 5 salary
from employee
order by case when name = 'abc' then 0 else 1 end, salary desc



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

aprichard
Yak Posting Veteran

62 Posts

Posted - 2009-04-23 : 02:35:50
Try this, it may help you

SELECT TOP 5 SALARY FROM EMPLOYEE
WHERE NAME='ABC'
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-04-23 : 02:47:26
I Think here u get ur answer
CROSS POST

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=124238


iF theRe iS a wAy iN tHen theRe iS a wAy oUt..
Go to Top of Page

aprichard
Yak Posting Veteran

62 Posts

Posted - 2009-04-23 : 02:48:48
select * from @table
where id in (
select top 3 id from @table)
order by case when name='a' then 0 else 1 end
Go to Top of Page

aprichard
Yak Posting Veteran

62 Posts

Posted - 2009-04-23 : 02:49:58
Try it

declare @table table(ID int identity(1,1), Name char(1), sal int)
insert into @table values('b',5000)
insert into @table values('a',3000)
insert into @table values('c',8000)
insert into @table values ('d',2000)
insert into @table values ('a',8000)

select * from @table
where id in (
select top 3 id from @table)
order by case when name='a' then 0 else 1 end
Go to Top of Page
   

- Advertisement -