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 1 25
B 2 32
B 3 44
B 4 48
C 1 11
This appears to work
SELECT ID, Age
FROM Persons a
WHERE Seq = (SELECT MAX(Seq) FROM Persons b WHERE a.ID = b.ID)
But is this the best way, the only way? I don’t like using subqueries if I don’t have to and I recall you can use something but I forget what it is. Any idea?
Assuming SQL-Server ( >= 2005) or Oracle (10g?):
ROW_NUMBERreturns 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