title: SQL merging records based on date range
body: <p>I have a data set representing deliveries made by several vehicles, which can each load and discharge at different locations. I am trying to find out how many vehicles it took to do all deliveries, while grouping/merging all trips that loaded within +- x days. For example:</p>
  <ul>
  <li>vehicle V1 could load in Zone A (date X) and B (date X+2), and discharge in Zone C and D, that would count as 1 vehicle/trip because it loaded within 2 days.</li>
  <li>V1 could also simply do Zone A (date Y) to Zone C, that would count 1 trip as well</li>
  </ul>
  <p>This is how the raw data looks like:</p>
  <pre><code>    Id  LoadArea    LoadDate    Vehicle Discharge Area  Qty
  456559  Zone A      15/07/2019   V1     Zone Z          64
  408481  Zone B      13/07/2019   V1     Zone X          70
  365566  Zone A      12/07/2019   V1     Zone Y          26
  244634  Zone C      12/07/2019   V2     Zone X          10
  453409  Zone D      10/07/2019   V2     Zone Z          53
  183776  Zone A      20/04/2019   V1     Zone Y          13
  379698  Zone A      20/04/2019   V1     Zone Z          21
  370324  Zone D      28/02/2019   V3     Zone W          39
  </code></pre>
  <p>This is what I am aiming to get to:</p>
  <pre><code>LoadArea        LoadDate    Vehicle Qty
  Zone A;Zone B   12/07/2019  V1          160
  Zone C;ZoneD    10/07/2019  V2          63
  Zone A;Zone B   20/04/2019  V1          34
  Zone D          28/02/2019  V3          39
  </code></pre>
  <p>The LoadDate returned when merging records that are less than +- X days away would ideally be the earliest one of the records being merged.</p>
  <p>Would anyone have an idea how to do this - i've been stuck on this for days now.... Thanks for your help!</p>