By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,569 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,569 IT Pros & Developers. It's quick & easy.

SELECT statement using WHERE that involves value from "next" row?

P: n/a
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":

5
6
1
2
1
3
Jul 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.

Jul 19 '05 #2

P: n/a
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.

Jul 19 '05 #3

P: n/a
I do have an autoincrementing 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1='bar'

Aggro <sp**********@yahoo.com> wrote in message news:<DA************@read3.inet.fi>...
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.

Jul 19 '05 #4

P: n/a
go****@teamnemitoff.com (Adam Nemitoff) wrote in message news:<b4**************************@posting.google. com>...
I do have an autoincrementing 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1='bar'

Aggro <sp**********@yahoo.com> wrote in message news:<DA************@read3.inet.fi>...
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.


There is a solution, but you need to think carefully about why you are
having this difficulty - working with databases requires understanding
of a few concepts, eg, that rows are not in a particular order. What
happenes for example, if user A writes a row to the database and gets
id 5, but before he has written his following row, user B writes and
gets id 6 (this may happen in milliseconds). Even transactions will
not ensure that a given user gets sequential id numbers, because you
are bending a database concept. You must link rows by the metadata
that associates them, eg by having a 'ParentId', which is populated in
the second row with the id of the first row.

Anyway, the way I believe you may be able to implement your problem,
is preferably using the ParentId idea before, then joining a table to
itself, as follows:

SELECT fieldlist
FROM tablename t1, tablename t2
WHERE (t1.Id + 1)=t2.Id
AND t1.myField='1'
AND t2.myField='2'

Bear in mind however that this query will link ALL consecutive rows,
not just the pairs as they were created (eg, id 1 will be linked with
id2, and id 2 will be linked with id 3, etc).
Jul 19 '05 #5

P: n/a
On 17 Dec 2003 16:42:12 -0800, go****@teamnemitoff.com (Adam Nemitoff)
wrote:
I do have an autoincrementing 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1='bar'


If you do a "self join", you might be able to get what you are trying
for.

select * from mytable as table1, mytable as table2 where
table2.id = table1.id+1

Chuck Gadd
http://www.csd.net/~cgadd/aqua
Jul 19 '05 #6

P: n/a
I do have an autoincrementing 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1='bar'

Aggro <sp**********@yahoo.com> wrote in message news:<DA************@read3.inet.fi>...
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.

Jul 19 '05 #7

P: n/a
go****@teamnemitoff.com (Adam Nemitoff) wrote in message news:<b4**************************@posting.google. com>...
I do have an autoincrementing 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1='bar'

Aggro <sp**********@yahoo.com> wrote in message news:<DA************@read3.inet.fi>...
Adam Nemitoff wrote:
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?

ie. given a table with a single field named "myField" with the
following values I want a SELECT statement that selects the rows
"WHERE myField='1' and [NEXT ROW]myField = '2' ":


You _do_ know that rows _are not_ in any specific order in the database.
i.e. It is not giaranteed that they are in the same order you inserted
them? And query like that looks very suspicious, which might cause
problems in the near future _if_ you somehow would get it to work (I
don't know any way, except by doing two queries. )

However I might suggest you another way to solve this problem. What if
you would add another column to your table, which would hold that mystic
value from "next" row?

You might also want to add id-field to your table with auto_increment
value, so that you know in what order your rows were inserted, and the
"next row" would have some meaning.


There is a solution, but you need to think carefully about why you are
having this difficulty - working with databases requires understanding
of a few concepts, eg, that rows are not in a particular order. What
happenes for example, if user A writes a row to the database and gets
id 5, but before he has written his following row, user B writes and
gets id 6 (this may happen in milliseconds). Even transactions will
not ensure that a given user gets sequential id numbers, because you
are bending a database concept. You must link rows by the metadata
that associates them, eg by having a 'ParentId', which is populated in
the second row with the id of the first row.

Anyway, the way I believe you may be able to implement your problem,
is preferably using the ParentId idea before, then joining a table to
itself, as follows:

SELECT fieldlist
FROM tablename t1, tablename t2
WHERE (t1.Id + 1)=t2.Id
AND t1.myField='1'
AND t2.myField='2'

Bear in mind however that this query will link ALL consecutive rows,
not just the pairs as they were created (eg, id 1 will be linked with
id2, and id 2 will be linked with id 3, etc).
Jul 19 '05 #8

P: n/a
On 17 Dec 2003 16:42:12 -0800, go****@teamnemitoff.com (Adam Nemitoff)
wrote:
I do have an autoincrementing 'id' field

Thereform my query is semantically this:
SELECT ROW(id) WHERE item1='foo' AND ROW(id+1).item1='bar'


If you do a "self join", you might be able to get what you are trying
for.

select * from mytable as table1, mytable as table2 where
table2.id = table1.id+1

Chuck Gadd
http://www.csd.net/~cgadd/aqua
Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.