WITH cte as (
SELECT ClaimNumber,
ClaimStatus,
ROW_NUMBER() over (PARTITION BY ClaimNumber ORDER BY ActivityDate DESC) as rn
FROM @TempTable
)
SELECT t.*
FROM @TempTable t
JOIN (SELECT *
FROM cte
WHERE rn = 1) f
ON t.ClaimNumber = f.ClaimNumber
AND f.ClaimStatus = 0
UITVOER
Bovendien, als je maar 2 statussen kunt doen
WITH cte as (
SELECT ClaimNumber
FROM @TempTable
GROUP BY ClaimNumber
HAVING MAX(ClaimStatus) = 0
)
SELECT t.*
FROM @TempTable t
WHERE ClaimNumber IN (SELECT * FROM cte)