Retrieving the last record in each group There is a table messages that contains data as shown below: Id Name Other_Columns ------------------------- 1 A A_data_1 2 A A_data_2 3 A A_data_3 4 B B_data_1 5 B B_data_2 6 C C_data_1 If I run a query select * from messages group by name, I will get the result as: 1 A A_data_1 4 B B_data_1 6 C C_data_1 What query will return the following result? 3 A A_data_3 5 B B_data_2 6 C C_data_1 That is, the
undefined
Select first row in each GROUP BY group? As the title suggests, I'd like to select the first row of each set of rows grouped with a GROUP BY. Specifically, if I've got a purchases table that looks like this: SELECT * FROM purchases; id | customer | total ---+----------+------ 1 | Joe | 5 2 | Sally | 3 3 | Joe | 2 4 | Sally | 1 I'd like to query for the id of the largest purchase (total) made by each customer. Something like this: