sql select first record of each group

Solutions on MaxInterview for sql select first record of each group by the best coders in the world

showing results for - "sql select first record of each group"
Marla
10 Aug 2017
1On Oracle 9.2+ (not 8i+ as originally stated), SQL Server 2005+, PostgreSQL 8.4+, DB2, Firebird 3.0+, Teradata, Sybase, Vertica:
2WITH summary AS (
3    SELECT p.id, 
4           p.customer, 
5           p.total, 
6           ROW_NUMBER() OVER(PARTITION BY p.customer 
7                                 ORDER BY p.total DESC) AS rk
8      FROM PURCHASES p)
9SELECT s.*
10  FROM summary s
11 WHERE s.rk = 1
12Supported by any database:
13But you need to add logic to break ties:
14
15  SELECT MIN(x.id),  -- change to MAX if you want the highest
16         x.customer, 
17         x.total
18    FROM PURCHASES x
19    JOIN (SELECT p.customer,
20                 MAX(total) AS max_total
21            FROM PURCHASES p
22        GROUP BY p.customer) y ON y.customer = x.customer
23                              AND y.max_total = x.total
24GROUP BY x.customer, x.total
queries leading to this page
tsql select first row in each grouphow to select first row from each group in sqlselect first item in group sqlsql select the first record of each groupget first record in sql group by id sqlsql select first element of each grouphow to select the first row on group by in sqlfirst record in group by sqlsql server group by get first recordselect the first row in each group in sqlpick first in group by sqlsql get first row in each groupget first record of each group sqlhow to select first row in a group in sqlget first row of each group sqlsql get the first record of a groupselect first of each group sql serverget first value group by sqlsql select first group byselect first row from each group in sqlsql query get first record each groupsql get the first row for each groupsql select only first in groupstake only the first record from a group in sqlhow do you get the first record of each group in sql 3fsql select first record of each grouptake first record of group list from sqlsql select first row of groupwhich one to use 1st group by in sqlsql group by and get first recordselect first row for each group sqlms sql select first group bysql server select first record in a groupsql group by select first of table for each groupselect only first row when grouping by sqlpick first row of group sqlselect first row group by sql serversql get first row for each groupsql group by take firstgroup by select first sql serverselect first one from each group sqlsql get first row of each groupt sql first row with group bysql select first record of a groupsql group by select first recordselect first row from group by sql servergroup by and retrieve first records in sql serversql select first occurrence in each groupselect first record group by sqlsql select first value in grouphow do i get the first record of a group in sql 3fsql select first row in groupsql select first row in each groupsql first record in groupget first row of each group data sqlselect first from group sqlget first value in group by sqlselect first row value in group sqltsql first row in group byfind the first of each group sqlsql select first of a groupsql first row by groupsql server group by select first recordsql select only first row from groupsql select first record of each group