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

后端 未结 9 1001
别跟我提以往
别跟我提以往 2020-12-29 09:26

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 resul

9条回答
  •  感动是毒
    2020-12-29 10:12

    Just group on Players.Nick alone, and select the first (min) of the description

    SELECT     Players.Nick, MIN(Reasons.Description), SUM(Marks.Value) AS Sum
    FROM         Marks INNER JOIN
                          Players ON Marks.PlayerID = Players.ID INNER JOIN
                          Reasons ON Marks.ReasonId = Reasons.ID
    GROUP BY Players.Nick
    ORDER BY Players.Nick, Sum DESC
    

    that is if you always want the first without knowing it

提交回复
热议问题