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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:49:25+00:00 2026-06-06T03:49:25+00:00

Let’s say I have the following table: Value Time 0 15/06/2012 8:03:43 PM 1

  • 0

Let’s say I have the following table:

Value    Time
0        15/06/2012 8:03:43 PM
1        15/06/2012 8:03:43 PM     *
1        15/06/2012 8:03:48 PM 
1        15/06/2012 8:03:53 PM
1        15/06/2012 8:03:58 PM     
2        15/06/2012 8:04:03 PM     *
2        15/06/2012 8:04:08 PM
3        15/06/2012 8:04:13 PM     *
3        15/06/2012 8:04:18 PM
3        15/06/2012 8:04:23 PM
2        15/06/2012 8:04:28 PM     *
2        15/06/2012 8:04:33 PM     

How do I select the starred rows, that is, where Value has changed? Basically I’m trying to find the time when Value has changed so I can do other queries based on those time intervals. The solution shouldn’t depend on knowing Value or Time in advance.

It seems to me that this shouldn’t be very hard (but it’s hard enough for me apparently!).

I’m currently using SQL Server 2008 although I have access to 2012 if the new window/analytic functions are helpful.

I tried adapting the solutions here http://blog.sqlauthority.com/2011/11/24/sql-server-solution-to-puzzle-simulate-lead-and-lag-without-using-sql-server-2012-analytic-function/ but my query didn’t complete after an hour! I think the joins explode the row size to something unmanageable (or I screwed it up).

I can solve this problem with C# code and multiple db calls, but it seems like something that could be done in a table-valued function or SP which would be much nicer.

Also, a solution that only works with increasing Value is OK if that is easier.

  • 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-06T03:49:26+00:00Added an answer on June 6, 2026 at 3:49 am

    I think this is what you’re after:

    ;WITH x AS
    (
      SELECT value, time, rn = ROW_NUMBER() OVER 
      (PARTITION BY Value ORDER BY Time)
      FROM dbo.table
    )
    SELECT * FROM x WHERE rn = 1;
    

    This may be slow if the resultset is large and there isn’t a good supporting index…

    EDIT

    Ah, wait a second, the values go up and down, not just up… if that is the case you can try this much slower approach:

    DECLARE @x TABLE(value INT, [time] DATETIME)
    
    INSERT @x VALUES
    (0,'20120615 8:03:43 PM'),--
    (1,'20120615 8:03:43 PM'),--*
    (1,'20120615 8:03:48 PM'),--
    (1,'20120615 8:03:53 PM'),--
    (1,'20120615 8:03:58 PM'),--
    (2,'20120615 8:04:03 PM'),--*
    (2,'20120615 8:04:08 PM'),--
    (3,'20120615 8:04:13 PM'),--*
    (3,'20120615 8:04:18 PM'),--
    (3,'20120615 8:04:23 PM'),--
    (2,'20120615 8:04:28 PM'),--*
    (2,'20120615 8:04:33 PM');
    
    ;WITH x AS
    (
      SELECT *, rn = ROW_NUMBER() OVER (ORDER BY time)
      FROM @x
    )
    SELECT x.value, x.[time]
    FROM x LEFT OUTER JOIN x AS y
    ON x.rn = y.rn + 1
    AND x.value <> y.value
    WHERE y.value IS NOT NULL;
    

    Results:

    value  time
    -----  -----------------------
    1      2012-06-15 20:03:43.000
    2      2012-06-15 20:04:03.000
    3      2012-06-15 20:04:13.000
    2      2012-06-15 20:04:28.000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let's say you have a method that expects a numerical value as an argument.
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have such MYSQL table for logins: id---name---location---login_date ---------------------------------- 1----mike----usa-----21.08.2012 2----tony----uk------22.08.2012 3----tony----uk------23.08.2012
Let's say I have a table that lists various toys and the types of
Let me explain best with an example. Say you have node class that can
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have multiple requirements for a password. The first is that the

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.