I\'m trying to select only the row with the highest seq for each ID
ID | Seq | Age
-------------------
A 1 20
A 2 30
B
Assuming SQL-Server ( >= 2005) or Oracle (10g?):
WITH CTE AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Seq DESC) AS RN
, ID, Age
FROM
Persons
)
SELECT ID, Age
FROM CTE
WHERE RN = 1
ROW_NUMBER returns the sequential number of a row within a partition of a result set.
Edit: works also in Oracle as you can see here: http://sqlfiddle.com/#!4/b7e79/2/0