title: Nested query and grouping by unions (SQL)
body: <p>I need help writing a query to get some information but I am having trouble writing it.</p>
  
  <pre><code>[table_People]
  int id
  var name
  
  [table_Tools]
  int id
  var name
  
  [table_Activity1]
  int person_id
  int tool_id
  date delivery_date
  
  [table_Activity2]
  int person_id
  int tool_id
  date installation_date
  </code></pre>
  
  <p>The query needs to return a list of all people and the name of the most recent tool they used in either activity 1 or 2 (the most recent activity that happened between the two).</p>
  
  <pre><code>SELECT
      people.id   AS personId,
      people.name AS personName,
      (
          SELECT
              tools.name AS toolName
          FROM
              activity1
          JOIN
              tools ON tools.id=activity1.tool_id
          WHERE
              activity1.id=people.id
          UNION ALL
          SELECT
              tools.name AS toolName
          FROM
              activity2
          JOIN
              tools ON tools.id=activity2.tool_id
          WHERE
              activity2.id=people.id
          ORDER BY
              installationDate,deliveryDate
      ) AS toolName
  FROM
      people
  ORDER BY
      people.name
  ASC
  </code></pre>
  
  <p>The problem I am having is that I can't sort by date (delivery or installation) as I get errors because they are different column names.</p>