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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:22:15+00:00 2026-06-05T10:22:15+00:00

I need to compare 2 consecutive rows in the same table. If the data

  • 0

I need to compare 2 consecutive rows in the same table. If the data is missing in the second row, I should update it with the first row’s data.

For Eg:

Row   EmpID  DATE        PosID        EmpStatus  EmpDept  EmpVP
----------------------------------------------------------------------
1     21     2010-12-31  NULL         TC         NULL     40
2     21     2010-01-25  90156840101  NULL       407      NULL
3     21     2003-11-25  NULL         AC         NULL     NULL

First Iteration: Since Row1 EmpStatus = TC, I want to Update the EmpStatus on Row2 to TC (Since its NULL) and EmpVP to 40 on Row2, as shown below:

Row   EmpID  DATE        PosID        EmpStatus  EmpDept  EmpVP
----------------------------------------------------------------------
1     21     2010-12-31  NULL         TC          NULL    40
2     21     2010-01-25  90156840101  TC          407     40 
3     21     2003-11-25  NULL         AC          NULL    NULL

Second Iteration: Since PositionID is NULL on Row3, I want to update Row3 with the PositionID of Row2. Since Row2 now has EmpStatus = TC, I want to compare the row2 and row3 data. Since Row3 has a new value, I want to retain the new value “AC”. But at the same time I want to update the value of EmpDept of Row3 = 40 since it’s NULL. The desired results are shown below:

Row   EmpID  DATE        PosID        EmpStatus  EmpDept  EmpVP
----------------------------------------------------------------------
1     21     2010-12-31  NULL         TC          NULL    40
2     21     2010-01-25  90156840101  TC          407     40 
3     21     2003-11-25  90156840101  AC          407     40

I am working on historical data load and I have to build records going backwards in terms of Dates.

Can anyone please tell me how to code this?
I want to know if we can do these updates, preferably without using cursors, as I have a lot of employees in this table.

Thanks a lot!

  • 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-06-05T10:22:17+00:00Added an answer on June 5, 2026 at 10:22 am

    This is an iterative process which depends not only on each row visited, but also on data changes made during the processing. I can’t even begin to imagine how you’d do it without cursors.

    I think once you resolve yourself to using cursors, then it’s just a matter of determining the rules (in a more general fashion than you’ve presented them) and then implementing those rules using cursors.

    This appears to be a one-time process, so I’m not sure I understand why you’d be concerned about writing a routine that uses cursors to process the data, and running it during a maintenance period or just during a slow-usage period.

    Here’s some pseudocode for how you might approach this problem (you’ll need to get the actual syntax details from a manual and you’ll need to make sure your business rules are being applied in the right order and the right manner):

    BEGIN

    DECLARE @row NUMBER, @posID VARCHAR(20), @empStatus VARCHAR(2), @empDept NUMBER, @empVP NUMBER;
    DECLARE @prevPosID VARCHAR(20), @prevEmpStatus VARCHAR(2), @prevEmpDept NUMBER, @prevEmpVP NUMBER;
    DECLARE @isFirst NUMBER;
    SET @isFirst = 1;
    
    DECLARE  mycurs CURSOR FOR
    SELECT   Row, PosID, EmpStatus, EmpDept
    FROM     Empl
    ORDER BY Date DESC
    FOR UPDATE OF Empl;
    
    OPEN mycurs;
    
    FETCH NEXT mycurs INTO @row, @posID, @empStatus, @empDept, @empVP;
    
    WHILE (@@FETCH_STATUS != NOTFOUND)
    BEGIN
        IF (@isFirst == 1) THEN
        BEGIN
            @isFirst = 0;
        END
        ELSE
        BEGIN
            IF (@empStatus IS NULL AND @prevEmpStatus IS NOT NULL) THEN
            BEGIN
                SET @empStatus = @prevEmpStatus;
                UPDATE Empl SET EmpStatus = @empStatus WHERE Row = @row;
            END
            IF (@posID IS NULL AND @prevPosID IS NOT NULL) THEN
            BEGIN
                SET @posID = @prevPosID ;
                UPDATE Empl SET PosID = @posID WHERE Row = @row;
            END
            IF (@empDept IS NULL AND @prevEmpDept  IS NOT NULL) THEN
            BEGIN
                SET @empDept = @prevEmpDept;
                UPDATE Empl SET EmpDept= @empDept WHERE Row = @row;
            END
        END
    
        SET @prevPosID = @posID;
        SET @prevEmpStatus = @empStatus;
        SET @prevEmpDept = @empDept;
        SET @prevEmpVP = @empVP;
    
        FETCH NEXT mycurs INTO @row, @posID, @empStatus, @empDept, @empVP;
    END
    
    DEALLOCATE mycurs;
    CLOSE mycurs;
    

    END

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

Sidebar

Related Questions

I need to compare multiple columns from then same row in a table For
I need compare two date. One is from databese, second is from now. var
I need to compare numeric data from two datatables with the same schema. For
I need to compare dozens of fields in two objects (instances of the same
I need to compare two Date s (e.g. date1 and date2 ) and come
I need to compare a int value with a datetime in a SQLExpress table.
I need to compare whether date is less than 3 months old. I will
I need to compare two row with each other and then write the fields
I need to compare the windows registry data before and after install a program
I need to compare 2 tables, where one has the data and the other

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.