title: Delete and keep certain rows based on conditions from table in postgresql
body: <p>I have a table with this structure :</p>
  <pre><code>create table content_relations (
      mainConId    Integer not null,
      relatedConId Integer not null,
      similarity   float not null,
      relatedConAddedOn TIMESTAMP WITH TIME ZONE Not null);
  </code></pre>
  <p>Now I want to have a query by which I should be able to delete rows from this table with these conditions :</p>
  <pre><code>delete rows where count of same mainConId exceeds a max limit(say CMax), 
  i.e only keep CMax items per mainConId and that too with sorting according to similarity desc
  i.e. keep only CMax items which are most similar in this table and remove extraneous  records for every mainConId
  </code></pre>
  <p>So after running this query I should have at-most n*CMax rows in this table where n is number of unique mainConId.</p>
  <p>Can someone help me with the query ? I think it should be possible to do with postgres.
  Thanks in adv.</p>