title: Compare and list differences with previous row from same table
body: <p>I am trying to compare column values with previous subsequent row column values from the same table. I have seen some solutions online but couldn't find to address my need.</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[compare_results_by_id] 
      @history_id int
  AS
  BEGIN
  --declare table to insert old value and new value
  declare @HistoryLog table (ColumnName varchar(100), OldValue varchar(max), NewValue varchar(max), updated_by int)
  
  declare @previousID int
  
      SELECT [history_id]
            ,[first_name]
            ,[last_name]
            ,[occupation]
            ,[updated_by]
        FROM History_Table where history_id = @history_id
  
  -- get previous row 
  
      SELECT @previousID  = P.PreviousID
      FROM
      (SELECT  MAX(D.history_id) PreviousID
      FROM History_Table D
      WHERE informant_history_id &lt; @history_id) P
  
  -- TODO Compare results
  END
  </code></pre>
  
  <p>How do I loop through and compare column values from two rows? Is there any better approach? Please let me know.</p>