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
Does SQL Server compact support windowed functions?
Alternative 1--Will include all rows that tie. Will not include a row, if the only rows for a given Thread all have null for HitCount:
SELECT Thread, Function, HitCount
FROM (SELECT Thread, Function, HitCount,
MAX(HitCount) over (PARTITION BY Thread) as MaxHitCount
FROM Samples
WHERE FunctionId NOT IN
(SELECT CalleeId FROM Callers)) t
WHERE HitCount = MaxHitCount
ORDER BY ThreadId, HitCount DESC
Alternative 2--Will include all rows that tie. If there is no row for a given thread with non-null HitCount, will return all rows for that thread:
SELECT Thread, Function, HitCount
FROM (SELECT Thread, Function, HitCount,
RANK() over (PARTITION BY Thread ORDER BY HitCount DESC) as R
FROM Samples
WHERE FunctionId NOT IN
(SELECT CalleeId FROM Callers)) t
WHERE R = 1
ORDER BY ThreadId, HitCount DESC
Alternative 3--Will non-determistically pick one row in case of ties and discard others. Will include a row if all rows for a given thread have null HitCount
SELECT Thread, Function, HitCount
FROM (SELECT Thread, Function, HitCount,
ROW_NUMBER() over (PARTITION BY Thread ORDER BY HitCount DESC) as R
FROM Samples
WHERE FunctionId NOT IN
(SELECT CalleeId FROM Callers)) t
WHERE R = 1
ORDER BY ThreadId, HitCount DESC
Alternative 4 & 5--Uses older constructs, if the windowed functions aren't available, and says what is meant a little cleaner than using joins. Benchmark if spead is a priority. Both return all rows that participate in a tie. Alternative 4 will HitCount is null when non-null values are not available for HitCount. Alternative 5 will not return rows with HitCount is null.
SELECT *
FROM Samples s1
WHERE FunctionId NOT IN
(SELECT CalleeId FROM Callers)
AND NOT EXISTS
(SELECT *
FROM Samples s2
WHERE s1.FunctionId = s2.FunctionId
AND s1.HitCount < s2.HitCount)
ORDER BY ThreadId, HitCount DESC
SELECT *
FROM Samples s1
WHERE FunctionId NOT IN
(SELECT CalleeId FROM Callers)
AND HitCount =
(SELECT MAX(HitCount)
FROM Samples s2
WHERE s1.FunctionId = s2.FunctionId)
ORDER BY ThreadId, HitCount DESC