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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:30:21+00:00 2026-05-31T18:30:21+00:00

Given a table with employee statuses and effective dates, how can I retrieve just

  • 0

Given a table with employee statuses and effective dates, how can I retrieve just the data that reflects a change in status?

For example, given the following structure:

DECLARE @STATUSES TABLE(
  EMPLOYEE_ID INT NOT NULL,
  EFFECTIVE_DATE DATE NOT NULL,
  STATUS_CODE CHAR(1) NOT NULL
)
INSERT @STATUSES VALUES (1, '2012-01-01', 'A')
INSERT @STATUSES VALUES (1, '2012-02-28', 'A')
INSERT @STATUSES VALUES (1, '2012-03-01', 'T')
INSERT @STATUSES VALUES (2, '2012-01-01', 'A')
INSERT @STATUSES VALUES (2, '2012-02-14', 'A')
INSERT @STATUSES VALUES (2, '2012-03-10', 'A')
INSERT @STATUSES VALUES (3, '2012-02-01', 'A')
INSERT @STATUSES VALUES (3, '2012-03-17', 'A')
INSERT @STATUSES VALUES (3, '2012-03-18', 'T')
INSERT @STATUSES VALUES (3, '2012-04-01', 'A')
INSERT @STATUSES VALUES (4, '2012-03-01', 'A')

What query can be used to result in the following?

EMPLOYEE_ID     EFFECTIVE_DATE      STATUS_CODE
1               2012-01-01          A
1               2012-03-01          T
2               2012-01-01          A
3               2012-02-01          A
3               2012-03-18          T
3               2012-04-01          A
4               2012-03-01          A

In other words, I want to leave out those records that have the same employee id and status code as the one before it, if one exists with an earlier effective date. Notice that employee 1 is listed only twice because there were only two actual changes in status–the one on 2012-02-28 is inconsequential since the status didn’t change from the earlier date. Also notice that employee 2 is listed just once since his status never changed despite there being three records. Only the earliest date is shown for each change.

  • 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-31T18:30:22+00:00Added an answer on May 31, 2026 at 6:30 pm

    With some further experimenting, it looks like this will do what I want.

    ;WITH cte
         AS (SELECT ROW_NUMBER() OVER (PARTITION BY EMPLOYEE_ID ORDER BY EFFECTIVE_DATE) AS rownum
                    ,EMPLOYEE_ID
                    ,EFFECTIVE_DATE
                    ,STATUS_CODE
             FROM   @STATUSES)
    SELECT t2.EMPLOYEE_ID
           ,t2.EFFECTIVE_DATE
           ,t2.STATUS_CODE
    FROM   cte t2
           LEFT JOIN cte t1
             ON t2.EMPLOYEE_ID = t1.EMPLOYEE_ID
                AND t2.STATUS_CODE = t1.STATUS_CODE
                AND t2.rownum = t1.rownum + 1
    WHERE  t1.EMPLOYEE_ID IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a table or a temp table, I'd like to run a procedure that
I have a single table that shows employee deployments, for various types of deployment,
As I am working on Employee Management system, I have two table (for example)
Say I've employee table where any employee can be related to any other employee
I've just begun diving into data warehousing and I have one question that I
I am having one query given below in that Employee , ShiftAllocation , ShiftMaster
When two tables are given Employee Table EmpID Name 1 Jon 2 Smith 3
This question requires some hypothetical background. Let's consider an employee table that has columns
For a given table 'foo', I need a query to generate a set of
I am trying to link two fields of a given table to the same

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.