title: Run Postgres query that groups by one field and sorts by another
body: <p>I have a PostgreSQL table with the following relevant fields:</p>
  
  <pre><code>url
  title
  created_at
  </code></pre>
  
  <p>There can be many rows that contain identical URLs but different titles. Here are some sample rows:</p>
  
  <pre><code>www.nytimes.com | The New York Times         | 2016-01-01 00:00:00`
  www.wsj.com     | The Wall Street Journal    | 2016-01-03 15:32:13`
  www.nytimes.com | The New York Times Online  | 2016-01-06 07:19:08`
  </code></pre>
  
  <p>I'm trying to obtain an output that lists the following fields:</p>
  
  <p>1) <code>url</code><br>
  2) <code>title</code> that corresponds to the highest value of <code>created_at</code><br>
  3) count of all <code>title</code> for that unique <code>url</code></p>
  
  <p>So, output rows for the above sample would look something like this:</p>
  
  <pre><code>www.nytimes.com | The New York Times Online | 2
  www.wsj.com     | The Wall Street Journal   | 1
  </code></pre>
  
  <p>Based on the numerous SO posts I've read on similar questions, it looks like my best option for obtaining the first two fields (<code>url</code> and latest <code>title</code>) would be to use <code>DISTINCT ON</code>:</p>
  
  <pre><code>select distinct on (url) url, title from headlines order by url, created_at desc 
  </code></pre>
  
  <p>Likewise, to obtain the first and third fields (<code>url</code> and count of all <code>title</code>), I could simply use <code>GROUP BY</code>:</p>
  
  <pre><code>select url, count(title) from headlines group by url
  </code></pre>
  
  <p>What I can't figure out is how to combine the above methodologies and obtain the above-mentioned three values I'm trying to get.</p>
  
  <p>(Edited to provide more clarity.)</p>