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 6226269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:59:36+00:00 2026-05-24T08:59:36+00:00

Given a table (TableA) that contains the following data; Id Date Status RecordId 1

  • 0

Given a table (TableA) that contains the following data;

Id    Date    Status    RecordId
1    01/06/11    2      REC001
2    01/06/11    2      REC002
3    01/06/11    2      REC003
4    01/07/11    1      REC001

How can I return all records with a status of 2 except records with a given RecordId where a status of 2 is followed by a record of 1 at a later date (and there are no further records with a status of 2.

So for example, the query should return REC002 and REC003 as REC001 had a status of 2 in the past, but that was superseeded by record Id 4 with a status of 1 at a later date. If, at some later point in time, another record was added for REC001 with a status of 2, then this should again be present in the result set (assuming there are no later records with a status of 1).

My feeble attempt at messing about with this is;

DECLARE @TableA TABLE
(
    Id INT,
    Dt DATETIME,
    Stat INT,
    RecId VARCHAR(6)
)

INSERT INTO @TableA 

SELECT   1,    DATEADD(day, -5, current_timestamp),  2,   'REC001'
UNION
SELECT   2,    DATEADD(day, -4, current_timestamp),  2,   'REC002'
UNION
SELECT   3,    DATEADD(day, -3, current_timestamp),  2,   'REC003'
UNION
SELECT   4,    DATEADD(day, -2, current_timestamp),  1,   'REC001'

   SELECT * 
     FROM @TableA t1
LEFT JOIN @TableA t2 ON t1.RecId = t2.RecId 
    WHERE t1.Stat = 2 
      AND (t1.Dt >= t2.Dt 
      AND t2.Stat != 1)

This kinda works, but returns values where t1.Id = t2.Id. I know I can exclude this through my where clause, but if I add a bunch more records to the table it fails again. For example;

INSERT INTO @TableA 
SELECT   1,    DATEADD(day, -15, current_timestamp),  2,   'REC004'
UNION
SELECT   2,    DATEADD(day, -14, current_timestamp),  2,   'REC002'
UNION
SELECT   3,    DATEADD(day, -13, current_timestamp),  1,   'REC003'
UNION
SELECT   4,    DATEADD(day, -12, current_timestamp),  1,   'REC001'
UNION
SELECT   11,    DATEADD(day, -5, current_timestamp),  2,   'REC004'
UNION
SELECT   21,    DATEADD(day, -4, current_timestamp),  2,   'REC002'
UNION
SELECT   31,    DATEADD(day, -3, current_timestamp),  1,   'REC003'
UNION
SELECT   41,    DATEADD(day, -2, current_timestamp),  1,   'REC001'

Any ideas are appreciated.

EDIT: I tried the two answers given, and while neither gave me exactly what I needed, they certainly pointed me in the right direction. Using the answers given, I came up with the following that seems to do what I require;

;WITH lastSuccess(recid, dt) AS (
    select recid, max(dt) from @tableA
    where stat = 1
    group by recid
),
lastFailure(recid, dt) AS (
    select recid, max(dt) from @tableA
    where stat = 2
    group by recid
)
select a.* from @tablea a
-- Limit results to those that include a failure
INNER JOIN lastFailure lf ON lf.recid = a.recid AND lf.dt = a.dt
-- If the recid also has a success, show this along with it's latest success date
LEFT JOIN lastSuccess ls ON ls.recid = lf.recid 
-- Limit records to where last failure is > last success or where there is no last success.
WHERE (lf.dt > ls.dt OR ls.dt IS NULL)

The only drawback I can see here is if there are two records with exactly the same timestamp then it would appear in the result set twice. For example, if Id 21 was repicated as 22 then, it would appear twice. This isn’t a real problem as in reality, the timestamp will always be unique.

  • 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-24T08:59:36+00:00Added an answer on May 24, 2026 at 8:59 am
    WITH ranked AS (
      SELECT
        *,
        rn = ROW_NUMBER() OVER (PARTITION BY RecId ORDER BY Dt DESC)
      FROM TableA
    )
    SELECT
      r1.Id,
      r1.Dt,
      r1.Stat,
      r1.RecId
    FROM ranked r1
      INNER JOIN ranked r2 ON r1.RecId = r2.RecId AND r2.rn = 1
    WHERE r1.Stat = 2
    

    UPDATE after the question’s update

    WITH ranked AS (
      SELECT
        *,
        rn = ROW_NUMBER() OVER (PARTITION BY RecId ORDER BY Dt DESC)
      FROM TableA
    )
    SELECT
      Id,
      Dt,
      Stat,
      RecId
    FROM ranked
    WHERE Stat = 2 AND rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains the following data: +----+----------+ | ID | ParentID
This is for MySQL and PHP I have a table that contains the following
I have a table that contains tasks and I want to give these an
Given a table or a temp table, I'd like to run a procedure that
Given an HTML page that has a complex table-based layout and many tags that
Given an ASP.NET MVC view that generates a table of entries using a for
I have a <table/> in a ViewUserControl that I have given the tag a
I have a table which contains my server status create table ServerStatus ( ServerId
I have a table recording the amount of data transferred by a given service
I have the following bit of code that reads data from the an Oracle

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.