Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming the audit table also logs the <code>lease_period</code> primary key column, referenced here <code>lp_id</code> for simplicity, you could try the following approach:</p> <ol> <li><p>Find all the rows where <code>audit_type</code> is <code>'Updated'</code>.</p></li> <li><p>Rank all the rows by <code>audit_date</code> and partitioning them by <code>lp_id</code>.</p></li> <li><p>Rank the rows by <code>audit_date</code> partitioning by <code>lp_id, suite_id, lease_id, building_id</code>.</p></li> <li><p>Get the difference between the two rankings.</p></li> <li><p>Rank the rows again by <code>audit_date</code>, partitioning them now by <code>lp_id, suite_id, lease_id, building_id, (ranking_difference)</code>.</p></li> <li><p>Output all the rows where the last ranking value is 2 or greater.</p></li> </ol> <p>The first four steps result in a row set where each group of consecutive (in ascending order of <code>audit_date</code>) rows with identical values of <code>suite_id, lease_id, building_id</code> for the same <code>lp_id</code> will be uniquely distinguished by a value calculated as the difference between the rankings #2 &amp; #3.</p> <p>Within the group, every row, starting from the second one, will differ from the previous one only in the value of <code>tenant_trading_name</code>, which is just what we need. So, we rank the rows once again, taking into account the 'group ID' we've just obtained, then return every row with the ranking of 2 or higher.</p> <p>Here's an approximate implementation:</p> <pre><code>WITH marked AS ( SELECT *, grp = ROW_NUMBER() OVER (PARTITION BY lp_id ORDER BY audit_date) - ROW_NUMBER() OVER (PARTITION BY lp_id, suite_id, lease_id, building_id ORDER BY audit_date) FROM lease_period_audit WHERE audit_type = 'Updated' ), ranked AS ( SELECT *, rnk = ROW_NUMBER() OVER (PARTITION BY lp_id, suite_id, lease_id, building_id, grp ORDER BY audit_date) FROM marked ) SELECT audit_date, lp_id, tenant_trading_name, suite_id, lease_id, building_id FROM ranked WHERE rnk = 2 </code></pre> <p>Note. This assumes that the audit table logs only real changes, i.e. there can't be two consecutive rows with the same primary key where all four columns have identical values.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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