How do I select the first row per group in an SQL Query?

Posted by mafutrct on Stack Overflow See other posts from Stack Overflow or by mafutrct
Published on 2009-12-10T15:25:29Z Indexed on 2010/05/23 2:20 UTC
Read the original article Hit count: 285

Filed under:
|

I've got this SQL query:

SELECT   Foo, Bar, SUM(Values) AS Sum
FROM     SomeTable
GROUP BY Foo, Bar
ORDER BY Foo DESC, Sum DESC

This results in an output similar to this:

47  1	100
47  0	10
47  2	10
46  0	100
46  1	10
46  2	10
44  0	2

I'd like to have only the first row per Foo and ignore the rest.

47  1	100
46  0	100
44  0	2

How do I do that?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-ce