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
 General SQL Server Forums
 New to SQL Server Programming
 Select statement

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2012-02-17 : 12:19:10

In my table there is an ID field. what I want to do is select a record and a field from the next record.


ord_no oper_no type ID
1234 10 S 10
4333 20 O 11


I would like my select statement to return

ord_no oper_no NextOper type
1234 10 20 S


How do I modify my select statement to get what I am looking for?

select ord_no, oper_no, type
from sfdtlfil_sql
where type = 'S'

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-17 : 12:27:56
Only if you define what "Next" means

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2012-02-17 : 12:29:52
The ID increments by 1.
So the record I want has an ID of 10. I want the oper_no from the record that has an ID of 11
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-17 : 12:53:49
[code]

CREATE TABLE #myTable99(ord_no int, oper_no int, type char(1), ID int)
GO

INSERT INTO #myTable99(ord_no, oper_no, type, ID)
SELECT 1234, 10, 'S', 10 UNION ALL
SELECT 4333, 20, 'O', 11 UNION ALL
SELECT 1234, 10, 'S', 12 UNION ALL
SELECT 4333, 20, 'O', 13
GO

SELECT *
FROM #myTable99 l
LEFT JOIN #myTable99 r
ON l.ID = r.ID - 1
GO

DROP TABLE #myTable99
GO

[/code]


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -