title: mysql - if multiple rows are returned, then return back the one with the biggest date difference
body: <p>I have a fairly long query as shown below:</p>
  
  <pre><code>SELECT DISTINCT
          `pro`.`productId` AS `productId`,
          `c`.`lastName` AS `lastName`,
          (`b`.`bookingEnd` - INTERVAL IFNULL(`po`.`pickingOffsetMinutes`, 0) MINUTE) AS `pickAt`
  
      FROM
          ((((((((((`pk-booking-service`.`booking` `b`
          LEFT JOIN `pk-booking-service`.`flight` `f` ON (((`b`.`bookingId` = `f`.`bookingId`)
              AND (`f`.`flightDirectionId` = 2))))
          LEFT JOIN `pk-booking-service`.`customer` `c` ON ((`c`.`bookingId` = `b`.`bookingId`)))
          LEFT JOIN `pk-booking-service`.`customerVehicle` `cv` ON ((`b`.`bookingId` = `cv`.`bookingId`)))
          LEFT JOIN `pk-operation-service`.`PickingOffset` `po` ON ((`b`.`bookingId` = `po`.`bookingId`)))
          LEFT JOIN `pk-booking-service`.`vehicle` `v` ON ((`cv`.`vehicleId` = `v`.`vehicleId`)))
          LEFT JOIN `pk-asset-service`.`asset` `a` ON (((`b`.`bookingId` = `a`.`bookingId`)
              AND (`a`.`assetTypeId` = 2))))
          LEFT JOIN `pk-location-service`.`assetLocationState` `als` ON ((`a`.`assetId` = `als`.`assetId`)))
          LEFT JOIN `pk-location-service`.`location` `loc` ON ((`als`.`locationId` = `loc`.`locationId`)))
          LEFT JOIN `pk-product-service`.`bookingProduct` `pro` ON ((`pro`.`bookingId` = `b`.`bookingId`)))
          JOIN `pk-location-service`.`locationCluster` `locC` ON ((`locC`.`locationClusterId` = `loc`.`locationClusterId`)))
      WHERE
          ((`b`.`bookingStatusId` &lt;&gt; 2)
              AND (`b`.`checkingStatusId` = 2)
              AND (`pro`.`archived` = 0)
              AND  `b`.`siteId` = 24
              AND (`b`.`bookingEnd` &gt;= (CURDATE() + INTERVAL -(1) SECOND))
              AND (`b`.`bookingEnd` &lt;= (CURDATE() + INTERVAL 3 DAY)))
  </code></pre>
  
  <p>Which results the following:</p>
  
  <pre><code>productId .         lastName .  pickAt
  661,                FOX,        2020-01-11 22:22:00
  661,                Demo,       2020-01-01 11:11:00
  884,                Demo,       2020-01-03 11:11:00
  897,                Demo,       2020-01-02 11:11:00
  926,                Demo,       2020-01-05 11:11:00
  
  </code></pre>
  
  <p>The desired result I am looking for is:</p>
  
  <pre><code>productId .         lastName .  pickAt
  661,                FOX,        2020-01-11 22:22:00
  661,                Demo,       2020-01-01 11:11:00
  
  </code></pre>
  
  <p>Where by it selects the biggest time gap from the <code>pickAt</code> date time to now.</p>
  
  <p>I have tried a number of tries, like using <code>MAX</code> and altering my query to fit the above but had no luck. Suggestions?</p>