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 2000 Forums
 Transact-SQL (2000)
 Join table with itself

Author  Topic 

Togaspoon
Starting Member

42 Posts

Posted - 2003-02-26 : 18:17:50
I have a table with the following data.

 
Model Serial Date Meter
A 1 1/01/02 0
A 1 1/10/02 100
A 2 1/05/02 50
A 2 1/10/02 200
A 2 1/15/02 300

and I need a recordset like this...

Model Serial Date Meter NextMeter
A 1 1/01/02 0 100
A 1 1/10/02 100 NULL
A 2 1/05/02 50 200
A 2 1/10/02 200 300
A 2 1/15/02 300 NULL


Any help would be greatly appreciated.
TP

nr
SQLTeam MVY

12543 Posts

Posted - 2003-02-26 : 19:08:15
select Model, Serial, Date, Meter,
NextMeter = (select Meter from tbl t1 where t1.Serial = t.Serial and t1.Date = (select min(t2.Date) from tbl t2 where t2.Serial = t.Serial
and t2.Date > t.Date))
from tbl t
order by t.Serial, t.Date

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

Togaspoon
Starting Member

42 Posts

Posted - 2003-02-27 : 11:50:04
Thanks for quick reply NR!

Worked like a champ.

Go to Top of Page
   

- Advertisement -