×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

SQL: Find next record based on time column, after "code" column.
2

SQL: Find next record based on time column, after "code" column.

SQL: Find next record based on time column, after "code" column.

(OP)
Good Morning,
I want to query a table that contains multiple appointments with time stamped status code changes for each appointment. Specifically, I need to pull the immediate next record in time, after the last "IN" status. Below, you see that in this case, "MT" is the next code after the last "IN".

Appt Status Time_stamp
1234 YD 11/21/2018 8:00
1234 YD 11/21/2018 8:05
1234 YD 11/21/2018 8:20
1234 YD 11/21/2018 8:23
1234 YD 11/21/2018 8:30
1234 IN 11/21/2018 9:30
1234 IN 11/21/2018 9:45
1234 IN 11/21/2018 9:50
1234 MT 11/21/2018 11:05
1234 MT 11/21/2018 11:10
1234 MT 11/21/2018 11:13
1234 SHP 11/21/2018 12:55
1234 SHP 11/21/2018 12:56
1234 SHP 11/21/2018 12:58

RE: SQL: Find next record based on time column, after "code" column.

 Appt Status Time_stamp
 1234   YD   11/21/2018 8:00
 1234   YD   11/21/2018 8:05
 1234   YD   11/21/2018 8:20
 1234   YD   11/21/2018 8:23
 1234   YD   11/21/2018 8:30
 1234   IN   11/21/2018 9:30
 1234   IN   11/21/2018 9:45
 1234   IN   11/21/2018 9:50
 1234   MT   11/21/2018 11:05	
 1234   MT   11/21/2018 11:10
 1234   MT   11/21/2018 11:13
 1234  SHP   11/21/2018 12:55
 1234  SHP   11/21/2018 12:56
 1234  SHP   11/21/2018 12:58
 
Time_stamp of "the last "IN" status"

Select MAX(Time_stamp)
From MyTable
Where Appt = 1234
And Status = 'IN'


"the immediate next record in time"

Select *
From MyTable
Where Time_stamp >
(
Select MAX(Time_stamp)
From MyTable
Where Appt = 1234
And Status = 'IN'
)
Order By Time_stamp


and just take very first record from this outcome.

Or you could just take the MIN(Time_stamp) of the last Select statement and look for the record with that one Time_stamp

Select *
From MyTable
Where Time_stamp =
(Select MIN(Time_stamp)
From MyTable
Where Time_stamp >
(Select MAX(Time_stamp)
From MyTable
Where Appt = 1234
And Status = 'IN'))


---- Andy

There is a great need for a sarcasm font.

RE: SQL: Find next record based on time column, after "code" column.

CODE

DECLARE @MyTable TABLE (Appt int, Status varchar(5), Time_stamp datetime)
INSERT INTO @MyTable VALUES
(1234,'YD','11/21/2018 8:00'),
(1234,'YD','11/21/2018 8:05'),
(1234,'YD','11/21/2018 8:20'),
(1234,'YD','11/21/2018 8:23'),
(1234,'YD','11/21/2018 8:30'),
(1234,'IN','11/21/2018 9:30'),
(1234,'IN','11/21/2018 9:45'),
(1234,'IN','11/21/2018 9:50'),
(1234,'MT','11/21/2018 11:05'),	
(1234,'MT','11/21/2018 11:10'),
(1234,'MT','11/21/2018 11:13'),
(1234,'SHP','11/21/2018 12:55'),
(1234,'SHP','11/21/2018 12:56'),
(1234,'SHP','11/21/2018 12:58'),
 
 (234,'YD','11/21/2018 8:00'),
(234,'YD','11/21/2018 8:05'),
(234,'YD','11/21/2018 8:20'),
(234,'YD','11/21/2018 8:23'),
(234,'YD','11/21/2018 8:30'),
(234,'IN','11/21/2018 9:30'),
(234,'IN','11/21/2018 9:45'),
(234,'IN','11/21/2018 9:50'),
(234,'MT','11/21/2018 11:05'),	
(234,'MT','11/21/2018 11:10'),
(234,'MT','11/21/2018 11:13'),
(234,'SHP','11/21/2018 12:55'),
(234,'SHP','11/21/2018 12:56'),
(234,'SHP','11/21/2018 12:58')
 
Select MyTable.*
From (SELECT *, ROW_NUMBER() OVER (PARTITION BY Appt ORDER BY Time_Stamp) AS R_C
             FROM @MyTable) MyTable

INNER JOIN (SELECT *
            FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY Appt ORDER BY Time_Stamp) AS R_C
                        FROM @MyTable) MyTable
            WHERE Status = 'IN') MTbl ON  MTbl.Appt = MyTable.Appt
                                       AND MTbl.R_C = MyTable.R_C-1
                                       AND MyTable.Status <> 'IN' 

Borislav Borissov
VFP9 SP2, SQL Server

RE: SQL: Find next record based on time column, after "code" column.

(OP)
Thanks guys. I'll give these a try.

Mark

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close