title: Ecto fragment subquery with conditional where
body: <p>I'm trying to use ecto perform a query with a subquery as one of the <code>SELECT</code>s. In SQL it looks like this (<code>player has_many votes</code>):</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT
      players.id AS player_id,
      (SELECT count(*) FROM votes WHERE votes.player_id = players.id) AS vote_count
  FROM
      players
  </code></pre>
  
  <p>however, depending on the existence of a parameter I want the <code>SELECT</code> subquery to have an extra <code>WHERE</code> clause. E.g. </p>
  
  <pre><code>SELECT
      players.id AS player_id,
      (SELECT count(*) FROM votes WHERE votes.player_id = players.id AND votes.type = 'motm') AS vote_count
  FROM
      players
  </code></pre>
  
  <p>In ecto, I've come up with this:</p>
  
  <pre><code>vote_count_query =
    from(p in Player,
      select: %{
        player_id: p.id,
        vote_count:
          fragment(
            "SELECT count(*) FROM votes WHERE votes.player_id = ?",
            p.id
          )
      }
    )
  </code></pre>
  
  <p>Say there's a variable <code>vote_type</code> that may or may not be <code>nil</code>, how can I conditionally add a <code>where</code> clause to the inner select subquery? E.g.</p>
  
  <pre><code>fragment(
    "SELECT count(*) FROM votes WHERE votes.player_id = ? AND votes.type = ?",
    p.id,
    ^vote_type
  )
  </code></pre>
  
  <p>(If there's a better way of getting the count of votes for <strong>all</strong> players then I'd be glad to hear it. With a join it seems players for whom no votes exist are not returned.)</p>