SQL Query that Returns the Next Most Recent Record

I'm trying to write an SQL script to find the next to last record added to a table. The table in question (it's a table that tracks status changes to records that are in another table) has one row for every status change that is made to a record that is in the other table. So, there is a one to many relationship between the two tables. Also, every row in the status change table is date and time stamped.

For the sake of simplicity, let's call the status change table "TBL_B" and its "parent" table we can call "TBL_A". The 2 tables are related to one another through a single column (TBL_A.PONUM and TBL_B.PONUM).

What I am looking for is not the most recent record that was added to TBL_B for every record that is in TBL_A, but the next most recent. For example, assume that TBL_B has 4 rows in it for a single row in TBL_A as follows:

Row 1 was added on 1/1/2018
Row 2 was added on 2/1/2018
Row 3 was added on 3/1/2018
Row 4 was added on 4/1/2018

I want my query to return only Row 3

This is for a SQL Server DB

Any ideas / assistance / guidance is much appreciated
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Edit: Added tested example - You should be able to use an OUTER JOIN and ROW_NUMBER() to order the results by the date in DESC order. Then grab records for the second row only.

create table tbl_a (
  PoNum varchar(50)
create table tbl_b (
  PoNum varchar(50)
  , DateAdded datetime

insert into tbl_a values ('PO-A');
insert into tbl_b values
('PO-A', '4/1/2018')
,('PO-A', '3/1/2018')
,('PO-A', '2/1/2018')
,('PO-A', '1/1/2018')

;WITH data AS
       , b.DateAdded
FROM   data
WHERE  Row = 2

Open in new window

cbridgmanAuthor Commented:
The 2nd select statement is failing on an invalid object name "data". Is that the correct name or should I replace data with some other value?

Also, I am guessing that somecolumn and dataadded are from table B. Is that correct?
Sorry, please see my edit above.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

cbridgmanAuthor Commented:
Oh, wait a second. I excluded the ;WITH data AS piece by mistake. It appears that it ran when I added it in. Still have some testing to do though to ensure that it is returning the correct records. I will let you know.
cbridgmanAuthor Commented:
It appears that the query is returning the correct results now. Thanks very much for your help
cbridgmanAuthor Commented:
Running the SQL script that was suggested above
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.