Settings

Theme

SQL: Select only rows with Max Value on a Column

stackoverflow.com

6 points by adrianonantua 12 years ago · 2 comments

Reader

baxter001 12 years ago

Or those who happen to be using a dialect of sql with window functions, selecting the rows where:

ROW_NUMBER() OVER(PARTITION BY Id ORDER BY Rev DESC) AS "Row Number"

Returns 1 is clearer to my mind, with some caveats around dialects which use separate forms of row-number that assign identical/differing numbers to two consecutive identical rows.

goldenkey 12 years ago

I prefer SELECT table.* FROM table WHERE rev = (SELECT MAX(rev) FROM table WHERE id=table.id)

I hate joins. Subselects are so much clearer imo - and you don't end up with extra fields.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection