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

  • Home
  • SEARCH
  • 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 6699101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:39:21+00:00 2026-05-26T06:39:21+00:00

I have a SQL Server 2008 database with a history table for recording changes

  • 0

I have a SQL Server 2008 database with a history table for recording changes in a main table. I need to report the current (latest) value of the ‘rate’ column, and the most recent previous value that’s different from current.

So, given something like:

id   | rate   | uninteresting | updated_on   | version
-----+--------+---------------+--------------+----------
123  | 1.20   | foo           | 2010-10-18   | 1500
456  | 2.10   | bar           | 2010-10-12   | 2123
123  | 1.20   | baz           | 2010-10-10   | 1499
123  | 1.10   | baz           | 2010-10-08   | 1498
456  | 2.00   | bar           | 2010-10-11   | 2122
123  | 1.00   | baz           | 2010-08-01   | 1497
456  | 2.00   | quux          | 2010-10-05   | 2121
456  | 1.95   | quux          | 2010-09-07   | 2120

I want to produce:

id   | cur_rate | cur_ver | updated_on | prev_rate | prev_ver | prev_updated
-----+----------+---------+------------+-----------+----------+-------------
123  | 1.20     | 1500    | 2010-10-18 | 1.10      | 1498     | 2010-10-08
456  | 2.10     | 2123    | 2010-10-12 | 2.00      | 2122     | 2010-10-11

Note that I’m looking for the latest entry where the rate is different from the most recent entry.

I’ve tried various approaches, but either get way too many results, or none at all. Any suggestions?

  • 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-26T06:39:22+00:00Added an answer on May 26, 2026 at 6:39 am

    There are a couple of ways to accomplish this. Here’s one way

    Declare @table  as table( 
    id int, 
    rate decimal(10,5) , 
    uninteresting  varchar(10) ,
    updated_on  date, 
    version int )
    INSERT INTO @table
    VALUES 
    (123  , 1.20   , 'foo     ' , '2010-10-18'   , 1500),
    (456, 2.1, ' bar           ', ' 2010-10-12   ', 2123),
    (123, 1.2, ' baz           ', ' 2010-10-10   ', 1499),
    (123, 1.1, ' baz           ', ' 2010-10-08   ', 1498),
    (456, 2, ' bar           ', ' 2010-10-11   ', 2122),
    (123, 1, ' baz           ', ' 2010-08-01   ', 1497),
    (456, 2, ' quux          ', ' 2010-10-05   ', 2121),
    (456, 1.95, ' quux          ', ' 2010-09-07   ', 2120)
    
    
    ;WITH rates 
         AS (SELECT Row_number() OVER ( PARTITION BY curr.id, curr.rate ORDER BY curr.updated_on DESC) AS rn,
                curr.id, 
                curr.rate        cur_rate, 
                curr.version     cur_ver, 
                curr.updated_on, 
                previous.rate       prev_rate, 
                previous.version    prev_ver, 
                previous.updated_on prev_updated 
              FROM 
                    @table curr
                    LEFT JOIN @table previous
                    ON curr.id = previous.id 
                       AND curr.rate <> previous.rate
                       AND curr.updated_on > previous.updated_on 
    
       ) 
       SELECT 
               id, 
               cur_rate, 
               cur_ver, 
               updated_on, 
               prev_rate, 
               prev_ver, 
               prev_updated 
        FROM 
               rates 
        WHERE 
               rn = 1
    

    produces this result

    id          cur_rate cur_ver     updated_on prev_rate prev_ver    prev_updated
    ----------- -------- ----------- ---------- --------- ----------- ------------
    123         1.00000  1497        2010-08-01 NULL      NULL        NULL
    123         1.10000  1498        2010-10-08 1.00000   1497        2010-08-01
    123         1.20000  1500        2010-10-18 1.10000   1498        2010-10-08
    456         1.95000  2120        2010-09-07 NULL      NULL        NULL
    456         2.00000  2122        2010-10-11 1.95000   2120        2010-09-07
    456         2.10000  2123        2010-10-12 2.00000   2122        2010-10-11
    

    IF you change the rn to drop the rate in the partition by e.g.
    ( PARTITION BY curr.id ORDER BY curr.updated_on DESC) AS rn,

    you get

    id          cur_rate cur_ver     updated_on prev_rate prev_ver    prev_updated
    ----------- -------- ----------- ---------- --------- ----------- ------------
    123         1.20000  1500        2010-10-18 1.10000   1498        2010-10-08
    456         2.10000  2123        2010-10-12 2.00000   2122        2010-10-11
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in a sql server 2008 database that contains bunch of
I need to localize a SQL Server 2008 database. After investigating recommendations, I have
I have SQL Server 2008 database with two tables. The first table is called
I have a SQL Server 2008 database table that uses uniqueidentifier as a primary
I have a SQL Server 2008 database with a table that contains a FILESTREAM
I have a SQL Server 2008 database. This database has a Table called Book.
I have a SQL Server 2008 R2 database table with 12k address records where
I have a SQL Server 2008 database with the following information in a table:
I have a SQL Server 2008 database and a nvarchar(256) field of a table.
I have a SQL Server 2008 database. There is a table called Documents with

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.