title: Using a table alias/query expression in an Ecto fragment?
body: <p>Starting with this query as the basis for an Ecto version:</p>
  
  <pre><code> select folder_id, json_agg(p.*) 
   from folder_memberships inner join profiles p 
   on p.id=folder_memberships.profile_id 
   where folder_id in (1234) group by folder_id;
  </code></pre>
  
  <p>I've got this code:</p>
  
  <pre><code> # ids=[1234] 
   from(p in Profile,
     join: link in FolderMembership, on: link.profile_id == p.id,
     select: [link.folder_id, fragment("json_agg(?) as members", p)],
     group_by: link.folder_id,
     where: link.folder_id in ^ids
   ) 
   |&gt; Repo.all
  </code></pre>
  
  <p>And that gets me the following error:</p>
  
  <pre><code>== Compilation error on file lib/profile.ex ==
  ** (Ecto.Query.CompileError) variable `p` is not a valid query expression. 
  Variables need to be explicitly interpolated in queries with ^
  (ecto) expanding macro: Ecto.Query.select/3
  </code></pre>
  
  <p>I'm sure I'm missing elementary but I'm bonkered if I know what it is. I've tried a number of options, but all the examples I've been able to see do something like <code>fragment("json_agg(?)", p.some_field)</code>, not <code>p</code> itself.</p>