Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect rows where column value has changed
    primarykey
    data
    text
    <p>Let's say I have the following table:</p> <pre><code>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 </code></pre> <p>How do I select the starred rows, that is, where <code>Value</code> has changed? Basically I'm trying to find the time when <code>Value</code> has changed so I can do other queries based on those time intervals. The solution shouldn't depend on knowing <code>Value</code> or <code>Time</code> in advance.</p> <p>It seems to me that this shouldn't be very hard (but it's hard enough for me apparently!).</p> <p>I'm currently using SQL Server 2008 although I have access to 2012 if the new window/analytic functions are helpful.</p> <p>I tried adapting the solutions here <a href="http://blog.sqlauthority.com/2011/11/24/sql-server-solution-to-puzzle-simulate-lead-and-lag-without-using-sql-server-2012-analytic-function/">http://blog.sqlauthority.com/2011/11/24/sql-server-solution-to-puzzle-simulate-lead-and-lag-without-using-sql-server-2012-analytic-function/</a> 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).</p> <p>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.</p> <p>Also, a solution that only works with increasing <code>Value</code> is OK if that is easier.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload