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.
| 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 topfor ex:select top 5 salary from employee order by case when name='abc' then 0 else 1 endthis shows if name='abc' is in top.it will select either it will not in top 5But 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 salaryfrom employeeorder by case when name = 'abc' then 0 else 1 end, salary desc E 12°55'05.63"N 56°04'39.26" |
 |
|
|
aprichard
Yak Posting Veteran
62 Posts |
Posted - 2009-04-23 : 02:35:50
|
| Try this, it may help you SELECT TOP 5 SALARY FROM EMPLOYEEWHERE NAME='ABC' |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
|
|
aprichard
Yak Posting Veteran
62 Posts |
Posted - 2009-04-23 : 02:48:48
|
| select * from @tablewhere id in (select top 3 id from @table)order by case when name='a' then 0 else 1 end |
 |
|
|
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 @tablewhere id in (select top 3 id from @table)order by case when name='a' then 0 else 1 end |
 |
|
|
|
|
|