Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1082437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:17:57+00:00 2026-05-16T22:17:57+00:00

I have a table with rows like this: ID StatusId Date 1 1 2001-01-01

  • 0

I have a table with rows like this:

   ID  StatusId   Date
    1    1        2001-01-01
    2    1        2001-01-02
    3    2        2001-01-03
    4    3        2001-01-04
    5    1        2001-01-05
    6    2        2001-01-06
    7    2        2001-01-07
    8    1        2001-01-08
    9    1        2001-01-09

I need to get the date when the current value of the status was originally changed. For the above example, the last value is 1, and it’s changed in row 8, so the result would be 2001-01-08.

How would you do this?

If you need a table to test with, here it is:

DECLARE @Tbl AS TABLE (ID INT, StatusId INT, Date DATETIME)

INSERT INTO @Tbl(ID, StatusId, Date)
SELECT 1,1,'2001-01-01' UNION
SELECT 2,1,'2001-01-02' UNION
SELECT 3,2,'2001-01-03' UNION
SELECT 4,3,'2001-01-04' UNION
SELECT 5,1,'2001-01-05' UNION
SELECT 6,2,'2001-01-06' UNION
SELECT 7,2,'2001-01-07' UNION
SELECT 8,1,'2001-01-08' UNION
SELECT 9,1,'2001-01-09' 

    SELECT * FROM @Tbl
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:17:57+00:00Added an answer on May 16, 2026 at 10:17 pm

    This one should get you what you’re after:

    declare @LastStatusID int
    declare @LastDate datetime
    declare @LastID int
    
    declare @LastChangeID int
    
    /* get last record */
    select top 1 @LastStatusID = StatusID, @LastDate = Date, LastID = ID
    from @Tbl
    order by ID desc
    
    /* get last record with a different status */
    select top 1 @LastChangeID = ID
    from @Tbl
    where ID < @LastID and StatusID <> @LastStatusID
    order by ID desc
    
    /* get the first next record - this would get you the last record as well whe it's just been set */
    select top 1 Date
    from @Tbl
    where ID > @LastChangeID
    order by ID asc
    

    I haven’t included any checking for margin examples when there’d be just one record in the table or multiple of them but all with the same status. You can figure those out yourself.

    As a single query

    This query requires IDs without gaps and it will get you the last record after a status change and it will also work when there’s just one record in the table or multiple of them with the same status (isnull provides the required functionality)

    select top 1 Date
    from @tbl t1
        left join @tbl t2
        on (t2.ID = t1.ID - 1)
    where (isnull(t2.StatusID, -1) <> t1.StatusID)
    order by ID desc
    

    Last where clause changes a null value (when there’s no upper record) to -1. If you do have a status with this value, you should change this number to some non-existing status value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I have one JavaScript that creates rows in a table like this: function
I have a table that has rows like this: <tr id= class=objectRow> <td class=bulkSelector><input
I have a table that stores records like this: name | stat | value
I have a table with rows like this: | Country.Number | Country | |
I have a table that looks like this: Id GroupId Value and it has
I have a table that looks like that: The rows are sorted by CLNDR_DATE
I have a table that saves some account limits like users. For most rows
I have a table with more than a millon rows. This table is used
I have a table of about a million rows and I need to update
I have this table : And I would like to make a request that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.