title: POSTGRESQL : How to select the first row of each group?
body: <p>With this query : </p>
  
  <pre><code>WITH responsesNew AS
  (
    SELECT DISTINCT responses."studentId", notation, responses."givenHeart", 
    SUM(notation + responses."givenHeart") OVER (partition BY responses."studentId" 
    ORDER BY responses."createdAt") AS total, responses."createdAt",  
    FROM responses
  )
  SELECT responsesNew."studentId", notation, responsesNew."givenHeart", total, 
  responsesNew."createdAt"
  FROM responsesNew
  WHERE total = 3
  GROUP BY responsesNew."studentId", notation, responsesNew."givenHeart", total, 
  responsesNew."createdAt"
  ORDER BY responsesNew."studentId" ASC
  </code></pre>
  
  <p>I get this data table :</p>
  
  <pre><code>studentId | notation | givenHeart | total |      createdAt     |
  ----------+----------+------------+-------+--------------------+
   374      | 1        | 0          | 3     | 2017-02-13 12:43:03   
   374      | null     | 0          | 3     | 2017-02-15 22:22:17
   639      | 1        | 2          | 3     | 2017-04-03 17:21:30 
   790      | 1        | 0          | 3     | 2017-02-12 21:12:23
   ...
  </code></pre>
  
  <p>My goal is to keep only in my data table the early row of each group like shown below : </p>
  
  <pre><code>studentId | notation | givenHeart | total |      createdAt     |
  ----------+----------+------------+-------+--------------------+
   374      | 1        | 0          | 3     | 2017-02-13 12:43:03 
   639      | 1        | 2          | 3     | 2017-04-03 17:21:30 
   790      | 1        | 0          | 3     | 2017-02-12 21:12:23
   ...
  </code></pre>
  
  <p>How can I get there?</p>
  
  <p>I've read many topics over here but nothing I've tried with <code>DISTINCT</code>, <code>DISTINCT ON</code>, subqueries in <code>WHERE</code>, <code>LIMIT</code>, etc have worked for me (surely due to my poor understanding). I've met errors related to window function, missing column in <code>ORDER BY</code> and a few others I can't remember.</p>