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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to select Upto??

Author  Topic 

Sql_forum
Yak Posting Veteran

50 Posts

Posted - 2012-06-21 : 05:29:27
Hi all,
I have a table which has data looks as follows:

A B
1 10
1 10
1 10
3 15
3 15
3 15
5 18
5 18
5 18

Here B' column is filtered based on where condition

1. if the input i m passing in where condition is 12 then i want to select rows looks as below :
3 15
3 15
3 15

2. if the input is 17 , the data should fetch
5 18
5 18
5 18


Can any one help me??

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-06-21 : 05:55:22
select * from tbl where B = (select min(B) frrom tbl where B > @inpno)


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-06-21 : 07:29:15
Another way would be to use TOP clause as in
SELECT TOP (1) WITH TIES
A,B
FROM
tbl
WHERE
b > @inpno
ORDER BY
b ASC
Go to Top of Page
   

- Advertisement -