SQL: Select maximum value for each unique key?

前端 未结 3 904
盖世英雄少女心
盖世英雄少女心 2020-12-21 18:17

Sorry, I\'m not sure how to phrase that and I\'m really not very good with SQL. The db engine i SQL Server Compact. I currently have this query:

SELECT *
FRO         


        
3条回答
  •  眼角桃花
    2020-12-21 19:08

    Here's how I would do it:

    SELECT s1.*
    FROM Samples s1
    LEFT JOIN Samples s2 
      ON (s1.Thread = s2.Thread and s1.HitCount < s2.HitCount)
    WHERE s1.FunctionId NOT IN (SELECT CalleeId FROM Callers) 
      AND s2.Thread IS NULL
    ORDER BY s1.ThreadId, s1.HitCount DESC
    

    In other words, the row s1 for which there is no other row s2 matching the same Thread and having a greater HitCount.

提交回复
热议问题