0

I want to select the next record of a WHERE condition is met .

EG 
REC  DATA
10   Name1
20   Address1
30   Post1
10   Name2
20   Address2
30   Post2

Now my query is

select * from table where Data = 'Name1'

in the result I want REC20 and rec30 should also be displayed for Name1.

jarlh
  • 42,561
  • 8
  • 45
  • 63
user3855942
  • 75
  • 3
  • 10
  • Which DBMS are you using? Postgres? Oracle? –  May 26 '15 at 13:50
  • 8
    SQL tables represent *unordered* sets. There is no concept of "next row" unless a column explicitly contains the ordering. Do you have such a column? Also, you should tag your question with the database you are using. – Gordon Linoff May 26 '15 at 13:50
  • Most modern versions of RDBMS have functions like `LEAD()` or `LAG()` to enable you to access next or previous row. – Radu Gheorghiu May 26 '15 at 13:52
  • any simple query solution ? – user3855942 May 26 '15 at 13:53
  • Why do you want to do such a thing ? Is the DB already designed like this ? Is it a school assignment ? – thomasb May 26 '15 at 13:53
  • `any simple query solution ?` - no, there is no simple solution to this. It's not something you want to be doing anyway. – thomasb May 26 '15 at 13:53
  • 2
    @user3855942: Read Gordon's comment. Answer his question. – Thorsten Kettner May 26 '15 at 13:54
  • these are the only 2 columns @GordonLinoff – user3855942 May 26 '15 at 14:01
  • 1
    This looks like a traditional data file to me, not like a database table. So there are two main options: 1. Maybe you don't need a database for this, then go on working with a data file instead (where records are implicitly ordered, because you read it line after line). 2. If you want a database, then design your tables properly and convert your data accordingly. – Thorsten Kettner May 26 '15 at 14:01
  • 1
    @user3855942: Then there is nothing that links Name1 to Address1. They are completely unrelated in your database table. (To get an idea about it, make six slips of paper and write a record on each. Then put them in a bucket. Now find a rule which paper to pick for the address belonging to Name1.) – Thorsten Kettner May 26 '15 at 14:02

0 Answers0