title: Match transaction_start for post voided receipts with transaction_start from the original receipt
body: <p>Match post voided receipts with original receipts:</p>
  <p>I have a <code>transaction</code> table that looks like this:</p>
  <pre><code>transaction_start        store_no        item_no        amount        post_voided
  2021-02-28 08:00:00           001            101            45                  N
  2021-02-28 08:00:00           001            105            25                  N
  2021-02-28 08:00:00           001            109            40                  N
  2021-03-01 10:00:00           001            101            45                  N
  2021-03-01 10:00:00           001            105            25                  N
  2021-03-01 10:00:00           001            109            40                  N
  2021-03-01 10:02:00           002            101            45                  N
  2021-03-01 10:02:00           002            105            25                  N
  2021-03-01 10:02:00           002            109            40                  N
  2021-03-01 10:04:00           001            101            45                  N
  2021-03-01 10:05:00           002            103            35                  N
  2021-03-01 10:05:00           002            135            20                  N
  2021-03-01 10:08:00           001            140            2                   N
  2021-03-01 10:11:00           001            101           -45                  Y
  2021-03-01 10:11:00           001            105           -25                  Y
  2021-03-01 10:11:00           001            109           -40                  Y
  </code></pre>
  <p>Each receipt/purchase is a combination of the <code>transaction_start</code> and <code>store_no</code>. Each receipt can have one or more <code>item_no</code> (and the respective price given by the <code>amount</code> variable). If the receipt has two or more items, the <code>transaction_start</code> and <code>store_no</code> values are repeated. So in the table above, a total of 7 receipts can be observed: receipt 1 is given by <code>transaction_start</code> = '2021-02-28 08:00:00' and <code>store_no</code> = '001', and contains the following items: 101, 105 and 109; receipt 2 by <code>transaction_start</code> = '2021-03-01 10:00:00' and <code>store_no</code> = '001', containing the following items: 101, 105 and 109, and so on.</p>
  <p>The last receipt (<code>transaction_start</code> = '2021-03-01 10:11:00' and <code>store_no</code> = '001') is a post voided of the second receipt (<code>transaction_start</code> = '2021-03-01 10:00:00' and <code>store_no</code> = '001'). The post voided indication is given by the column and value <code>post_voided</code> = 'Y'.</p>
  <p>My goal is to change the <code>transaction_start</code> for the <code>post_voided</code> = 'Y' receipts for the original <code>transaction_start</code> datetime. By original receipt, it is meant that all <code>item_no</code> and (negative) <code>amount</code> from the post voided receipt (i.e. <code>post_voided</code> = 'Y') must match with the closest (i.e. equal or lower) <code>transaction_start</code>, <code>store_no</code> receipt contaitning the same <code>item_no</code>, (positive) <code>amount</code> and not post voided (o.e. <code>post_voided</code> = 'N').</p>
  <p>Desired output:</p>
  <pre><code>transaction_start        store_no        item_no        amount        post_voided
  2021-02-28 08:00:00           001            101            45                  N
  2021-02-28 08:00:00           001            105            25                  N
  2021-02-28 08:00:00           001            109            40                  N
  2021-03-01 10:00:00           001            101            45                  N
  2021-03-01 10:00:00           001            105            25                  N
  2021-03-01 10:00:00           001            109            40                  N
  2021-03-01 10:02:00           002            101            45                  N
  2021-03-01 10:02:00           002            105            25                  N
  2021-03-01 10:02:00           002            109            40                  N
  2021-03-01 10:04:00           001            101            45                  N
  2021-03-01 10:05:00           002            103            35                  N
  2021-03-01 10:05:00           002            135            20                  N
  2021-03-01 10:08:00           001            140            2                   N
  2021-03-01 10:00:00           001            101           -45                  Y
  2021-03-01 10:00:00           001            105           -25                  Y
  2021-03-01 10:00:00           001            109           -40                  Y
  </code></pre>
  <p>Here a link of the table: <a href="https://dbfiddle.uk/?rdbms=sqlserver_2019&amp;fiddle=06afe841d89c4ecbf600436433cd1c5d" rel="nofollow noreferrer">https://dbfiddle.uk/?rdbms=sqlserver_2019&amp;fiddle=06afe841d89c4ecbf600436433cd1c5d</a></p>
  <p>Thanks in advance!</p>