title: SQL query for finding representative rows in a table
body: <p>Let's say I have a payments table like so:</p>
  <pre><code>CREATE TABLE Payments (
    PaymentID INT,
    CustomerID INT,
    Value INT,
    PaidOn DATE
  );
  
  INSERT INTO Payments
    VALUES
  (1, 1, 5, '2000-01-01'),
  (2, 1, 10, '2000-02-01'),
  (3, 2, 10, '2000-01-02'),
  (4, 2, 10, '2000-01-20'),
  (5, 2, 5, '2000-02-02'),
  </code></pre>
  <p>And I want to run a query for the entire row for the maximum payment made by each customer.  Is this possible using a single SQL query, to avoid having to dig through every row that I'm not interested -- or worse, run a query for each customer?</p>
  <p>The best I have come up with so far is:</p>
  <pre><code>SELECT CustomerID, MAX(Value)
    FROM Payments
    GROUP BY CustomerID;
  </code></pre>
  <p>But this does not give me the <code>PaymentId</code> or <code>PaidOn</code> values for the rows it finds.</p>