title: count by on window function
body: <p>I have created this <a href="http://www.sqlfiddle.com/#!12/53cd9/3" rel="nofollow">sqlfiddle</a> which executes this query:</p>
  
  <pre><code>SELECT "emails".*
  FROM (
      SELECT "emails".id, sender_contact_id, sender_user_id,
      rank() OVER (PARTITION BY "sender_contact_id" ORDER BY "sent_at" DESC) AS "contact_rnk",
      rank() OVER (PARTITION BY "sender_user_id" ORDER BY "sent_at" DESC) AS "user_rnk"
      FROM "emails"
      WHERE ("folder" = 'INBOX')
  ) AS "e"
  INNER JOIN "emails"
  ON ("emails"."id" = "e"."id")
  WHERE ((contact_rnk = 1 or user_rnk = 1) AND folder = 'INBOX')
  ORDER BY sent_at DESC;
  </code></pre>
  
  <p>The point of the query is to return only 1 row for each contact or user that has sent an email.  </p>
  
  <p>What is missing from the query is a count of the emails that have been sent for each partitioned user or contact.</p>
  
  <p>I could do multiple with queries and join at the end e.g.</p>
  
  <pre><code>with main as (
    select blbah, etc.
  ),
  cnt_users as (
    select count(sender_user_id) inner join main etc.
  ),
  cnt_contacts as (
    select count(sender_contact_id) inner join main etc.
  )
  select main.*, cntusers etc.
  </code></pre>
  
  <p>But is it possible to include the count in the first query without having to use <code>group by</code>?</p>