title: Find changes made in PGSQL table using SQL in Excel VBA
body: <p>Is it possible to create an SQL query to compare a field within a single table to see if a change has been made and if possible list the before and after?</p>
  
  <p>I have the following SQL query written in Excel 2010 VBA, which connects to an Oracle PostGreSQL database</p>
  
  <pre class="lang-vb prettyprint-override"><code>Dim au As String
  
  au = "SELECT id, priority, flag, code " _
      &amp; "FROM hist WHERE ( aud_dt &gt;= '18/05/2020' AND aud_dt &lt;='18/05/2020' ) " _
  
  Set rs = conn.Execute(au)
  With ActiveSheet.QueryTables.Add(Connection:=rs, Destination:=Range("A1"))
      .Refresh
  End With
  </code></pre>
  
  <p>Where fields include:</p>
  
  <ul>
  <li>"priority" is the field that I'd like to check for changes which will
  be a single number between 0-9 </li>
  <li>"code" is the record that has been
  assigned the priority and is a mixture of numbers and letters up to 7
  characters </li>
  <li>"flag" shows a 1 as the active record, and 2 as an edited
  record </li>
  <li>"id" refers to the user account</li>
  </ul>
  
  <p>I'd ideally like to end up with something like: <code>id | priority | flag | priority_old | flag_old | code</code></p>
  
  <p>Which should show the before and after changes to the priority. If the record shows <code>priority=3 and flag=2 and code=Ab12</code>, there must also be record with a 1 flag, as that is now the active record. If it has the same priority number for the code I'm not interested in it as that just means something else was changed instead as I have not listed all the column fields. </p>
  
  <p>If the active record now shows <code>priority=4, flag=1 and code=Ab12</code>, that would be exactly the record I need to see.</p>