title: TypeORM SQL SubQuery
body: <p>I want to retrieve a list of maintenances, distincted by ids and sorted by date.</p>
  <p>Basically, I'm trying to translate this SQL query (under PostgreSQL) :</p>
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM 
  (
    SELECT DISTINCT m.id, * 
    FROM maintenances AS m 
    LEFT JOIN maintenance_items AS mi ON mi.maintenance_id = m.id 
    LEFT JOIN maintenance_types AS mt ON mi.type = mt.id 
    WHERE bike_id = '152cd134-c868-4a9e-ad49-691569f5786d'
  ) AS foo 
  ORDER BY foo.m_date;
  </code></pre>
  <p>as a TypeORM query. I tried this :</p>
  <pre><code>DbConnection.manager
      .createQueryBuilder&lt;Maintenance&gt;(Maintenance, 'maintenances')
      .select('*')
      .from(qb =&gt; qb.subQuery()
        .select('*')
        .from(Maintenance, 'maintenances')
        .where('maintenances.bikeID = :id', { id: id })
        .leftJoinAndSelect('maintenances.items', 'maintenance_items')
        .leftJoinAndSelect('maintenance_items.type', 'maintenance_types')
        .distinctOn(['maintenances.id']), 'foo')
      .orderBy('foo.m_date', 'DESC')
  
  </code></pre>
  <p>And I'm getting this error : Cannot get entity metadata for the given alias &quot;foo&quot;. Cannot figure out why.</p>