tags:

views:

161

answers:

6

Here is the SQL table:

KEY | NAME    | VALUE  
---------------------
13b | Jeffrey | 23.5  
F48 | Jonas   | 18.2  
2G8 | Debby   | 21.1

Now, if I type:

SELECT * 
  FROM table  
 WHERE VALUE = 23.5

I will get the first row.

What I need to accomplish is to get the first and the next two rows below. Is there a way to do it?

Columns are not sorted and WHERE condition doesn't participate in the selection of the rows, except for the first one. I just need the two additional rows below the returned one - the ones that were entered after the one which has been returned by the SELECT query.

A: 

If I understand correctly, you're looking for something like: SELECT * FROM table WHERE value <> 23.5

loginx
I think you mean `<= 23.5`.
OMG Ponies
No, i actually meant `<>` but I'm not even sure i understood the question correctly. My understanding is that he wants any value that is not equal to `23.5`.
loginx
The OP states they want the row, plus two additional ones.
OMG Ponies
Ahh you are correct. `<=` it is.
loginx
Thank you so much for the answer and the comments. Actually, I wanted the row returned by the query + the next two rows entered after the one returned by the query. It doesn't necessarily mean that the values of the VALUE column are smaller than the VALUE value of the returned row.
Boris
+6  A: 

Without a date column or an auto-increment column, you can't reliably determine the order the records were entered.

The physical order with which rows are stored in the table is non-deterministic.

RedFilter
+1 - beat me to it :)
AdaTheDev
This is a comment, not an answer to the OP's question.
OMG Ponies
*You can't* is certainly an answer.
RedFilter
The *ask for clarification* because of limitations - you're assuming it can't be done.
OMG Ponies
The OP asked for the rows "that were entered after the one which has been returned". That chronology cannot be established without a date field, autoincrement or similar. I don't see where clarification is needed...?
RedFilter
Thank you so much for the answer and the comments. Your clarifications have been very helpful. I understand now that I must have the data sorted by some colum in order to be able to achieve what I needed.
Boris
+1  A: 

If each row has an ID column that is unique and auto incrementing, you could do something like:

SELECT * FROM table WHERE id > (SELECT id FROM table WHERE value = 23.5)
Austin Fitzpatrick
SELECT TOP 3 * FROM table WHERE id > (SELECT id FROM table WHERE value = 23.5)
Kristian Damian
there is no guarantee that the ids will be in order. This will just base it off id number which isn't necessarily the first x 'above' or 'below' the found row(s).
Arthur Thomas
Then what do "above" and "below" even mean if not the date they were added to the table? What is the practical purpose of knowing their locations relative to each other in the table? Maybe I'm misunderstanding the question. An auto-incrementing ID is one way to sort things by date added. If you're worried about IDs being reset then you could look at adding a time stamp column, instead.
Austin Fitzpatrick
Thank you so much for the answer and the comments. Your clarifications have been very helpful. I understand now that I must have the data sorted by some colum in order to be able to achieve what I needed.
Boris
+1  A: 

You need to define an order to the results to do this. There is no guaranteed order to the data otherwise. If by "the next 2 rows after" you mean "the next 2 records that were inserted into the table AFTER that particular row", you will need to use an auto incrementing field or a "date create" timestamp field to do this.

AdaTheDev
Thank you so much for the answer. Your clarification has been very helpful.
Boris
A: 

You can obviously write a program to do that but i am assuming you want a query. What about using a Union. You would also have to create a new column called value_id or something in those lines which is incremented sequentially (probably use a sequence). The idea is that value_id will be incremented for every insert and using that you can write a where clause to return the remaining two values you want.

For example:

Select * from table where value = 23.5 Union Select * from table where value_id > 2 limit 2;

Limit 2 because you already got the first value in the first query

hmm
Thank you so much for your answer. Your clarification has been helpful. I understand now that an extra column is required.
Boris
A: 

You need an order if you want to be able to think in terms of "before" and "after".

Assuming you have one you can use ROW_NUMBER() (see more here http://msdn.microsoft.com/en-us/library/ms186734.aspx) and do something like:

With MyTable
(select row_number() over (order by key) as n, key, name, value
from table)

select key, name, value
from MyTable
where n >= (select n from MyTable where value = 23.5) 
Madalina Dragomir
Thank you so much for your answer.
Boris