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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:59:13+00:00 2026-06-09T05:59:13+00:00

I have a table in a SQL Server database which stores historical data on

  • 0

I have a table in a SQL Server database which stores historical data on daily basis. The structure is shown below:

UploadDate    TypeID    Value1   Value2
-------------------------------------------
2012-01-08    1         NEG      1998-02-05
2012-01-08    2         NEG      1999-02-09
2012-01-08    3         STABLE   1997-02-06
2012-02-08    1         NEG      1998-02-05
2012-02-08    2         NEG      1999-02-09
2012-03-08    1         POS      2012-03-08
2012-03-08    2         STABLE   2012-01-08

As you can see above for the TypeID 1 & 2, Value1 and Value2 has changed on 2012-03-08

My requirement is such that I have to show only those rows which have changed from previous values.

In this case since TypeID 1 & 2 have changed than it should show the current and most nearest previous value. And for TypeID 3 since it has not changed, it will will only show the most current values. The result set would look something like below:

UploadDate    TypeID    Value1   Value2
-------------------------------------------
2012-01-08    3         STABLE   1997-02-06
2012-02-08    1         NEG      1998-02-05
2012-02-08    2         NEG      1999-02-09
2012-03-08    1         POS      2012-03-08
2012-03-08    2         STABLE   2012-01-08

Any idea how I can tackle this using SQL?

  • 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-09T05:59:14+00:00Added an answer on June 9, 2026 at 5:59 am

    Uninspired version uses self-join on ordered set to check the value of chronologically previous row of the same typeid. If there is no previous row or values are different the row is output.

    ; with numbered as (
      select *,
             row_number() over (order by typeid, uploaddate) rn
        from table1
    )
    select n1.*
      from numbered n1
      left join numbered n2
        on n1.TypeID = n2.TypeID
       and n1.rn + 1 = n2.rn
     where (n2.rn is null 
        or n1.value1 <> n2.value1
        or n1.value2 <> n2.value2)
     order by typeid, uploaddate
    

    Here is Sql Fiddle with example.

    UPDATE: another variant which does not require self-join but does require group by. Each timeline of same typeid, value1 and value2 are given unique group_number which is used later to extract max(uploaddate) for the group.

    ; with numbered as (
      select *,
             row_number() over (order by typeid, uploaddate)
           - row_number() over (partition by typeid, value1, value2 
                                order by uploaddate) group_number
        from table1
    )
    select max(uploaddate) uploaddate, typeid, value1, value2
      from numbered
    group by typeid, value1, value2, group_number
    order by typeid, uploaddate
    

    Another Sql Fiddle.

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

Sidebar

Related Questions

I have a database table in Sql Server 2008 R2 which contains data stored
I have an EMPLOYEE table in a SQL Server 2008 database which stores information
I have table in my SQL Server database called OrderDetail which stores the order
I have a simple application which stores the data into the sql server database
I have a DataGridView which is filling from Table in SQL Server Database. One
Ok I have a table in my SQL Server database that stores comments. My
In my SQL Server database schema I have a data table with a date
I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
I have an INT column in a SQL Server database which stores a value
I have a simple database table (SQL Server 2008 R2 Express), which has a

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.