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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:02:43+00:00 2026-05-29T04:02:43+00:00

Consider the below Id Nums 1 10 2 20 3 30 4 40 5

  • 0

Consider the below

Id  Nums
1   10
2   20
3   30
4   40
5   50

The expected output

Id  CurrentValue    PreviousValue
1   10              Null
2   20              10
3   30              20
4   40              30
5   50              40

I am trying with the below but no luck

;With Cte(Id,CurrValue,PrevValue) As
(
    Select 
        Id
        ,CurrentValue = Nums 
        ,PreviousValue = Null
        From @t Where Id = 1
    Union All
    Select
        t.Id
        ,c.CurrValue
        ,c.PrevValue
    From Cte c
    Join @t t On c.Id <= t.Id + 1

)
Select *
From Cte

Help needed

  • 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-29T04:02:44+00:00Added an answer on May 29, 2026 at 4:02 am

    This assumes increasing ID values and will deal with gaps in ID

    SELECT
       ID, 
       This.Number AS CurrentValue, 
       Prev2.Number AS PreviousValue
    FROM
       myTable This
       OUTER APPLY
       (
        SELECT TOP 1
           Number
        FROM
           myTable Prev
        WHERE
           Prev.ID < This.ID  -- change to number if you want
        ORDER BY
           Prev.ID DESC
       ) Prev2;
    

    OR

    WITH CTE
         AS (SELECT ID,
                    Number,
                    ROW_NUMBER() OVER (ORDER BY ID) AS rn
             FROM   Mytable)
    SELECT ID,
           This.Number AS CurrentValue,
           Prev.Number AS PreviousValue
    FROM   CTE This
           LEFT JOIN CTE Prev
             ON Prev.rn + 1 = This.rn; 
    

    And for SQL Server 2012

    SELECT
       ID,
       Number AS CurrentValue,
       LAG(Number) OVER (ORDER BY ID) AS PreviousValue
    FROM
       MyTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the below Case 1: [Success] Input : X(P)~AK,X(MV)~AK Replace with: AP Output: X(P)~AP,X(MV)~AP
Consider the below SQL query INSERT INTO [dbo].[PartnerCommissionData] SELECT X.* FROM ( SELECT cs.partner_id
Consider the code below: ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null); ScriptRuntime runtime = new ScriptRuntime(setup); ScriptEngine
Consider the below ;WITH GetParentOfChild AS ( SELECT Rn = ROW_NUMBER() Over(Order By (Select
I remember I've read this from somewhere but still unsure. consider scenario below: <action
Consider the below: public class DependencyA {} public class DependencyB {} public class DependencyC
Consider the below situation I have a database table as below user_id user_rank ------------------
Let's consider the below example. There, I have: target MAIN calls target t and
Consider the script below. It will launch two subprocesses, each one a CherryPy app
Consider the code below, private void Convert_Click(Object sender, RoutedEventArgs e) { string[] strCmdLineParams =

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.