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 2005 Forums
 Transact-SQL (2005)
 Retrieve Data through priority

Author  Topic 

indraja
Starting Member

6 Posts

Posted - 2007-02-15 : 02:14:34
I have to pick values from a table.
Like if i have 1 i need to pick 2,if 2 is not there the next higher number like 3,4........

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2007-02-15 : 02:30:24
something like this maybe:

SELECT MIN(val)
FROM yourtable
WHERE val > seedvalue


seedvalue being 1 (as in your example). If 2 was not the next value available, it would grab the next highest as you want. Give it a shot.

btw, we can better answer these questions if you post DDL of your table, some sample data and and example of the desired output.


-ec
Go to Top of Page

indraja
Starting Member

6 Posts

Posted - 2007-02-15 : 03:45:33
quote:
Originally posted by eyechart

something like this maybe:

SELECT MIN(val)
FROM yourtable
WHERE val > seedvalue


seedvalue being 1 (as in your example). If 2 was not the next value available, it would grab the next highest as you want. Give it a shot.

btw, we can better answer these questions if you post DDL of your table, some sample data and and example of the desired output.


-ec







Sample Data

1
2
3
5
6
8
.
.
.


if i pick 1 i must get 2.
if i pick 2 i must get 3.
if i pick 3 then 4 is not there then i shud get 5
then so on.....





Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-02-15 : 04:27:12
SELECT TOP 3 Col1
FROM Table1
ORDER BY Col1


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-02-15 : 04:28:11
DECLARE @TopMost INT
SELECT @TopMost = 7

SELECT TOP (@TopMost) Col1
FROM Table1
ORDER BY Col1


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -