title: SQL Server Comparing Subsequent Rows for Duplicates
body: <p>I am trying to write a SQL Server query but have had no luck and was wondering if anyone may have any ideas on how to achieve my query.</p>
  
  <p>What i'm trying to do:</p>
  
  <p>I have a table with several columns naming the ones that i am dealing with TaskID, StatusCode, Timestamp. Now this table just holds tasks for one of our systems that run throughout the day and when something runs it gets a timestamp and the statuscode depending on the status for that task.</p>
  
  <p>Sometimes what happens is the task table will be updated with a new timestamp but the statusCode will not have changed since the last update of the task so for two or more consecutive rows of a given task the statusCode can be the same. When i say consecutive rows i mean with regards to timestamp.</p>
  
  <p>So example task 88 could have twenty rows at statusCode 2 after which the status code changes to something else. </p>
  
  <p>Now what i am trying to do with no luck at the moment is to retrieve a list from this table of all the tasks and the statuscodes and the timestamps but in the case where i have more than one consecutive row for a task with the same statuscode i just want to take the first row with the lowest timestamp and ignore the rest of the row until the statuscode for that task changes.</p>
  
  <p>To make it simpler in this case you can assume that i have a taskid which i am filtering on so i am just looking at a single task.</p>
  
  <p>Does anyone have any ideas as to how i can do this or perhaps something that i coudl probably read to help me?</p>
  
  <p>Thanks
  Irfan.</p>