SELECT te.[Status] AS [Status],
       COUNT(1)    AS [Count]
FROM [dbo].[TaskExecutions] te
WHERE te.[TriggerSource] = @ReviewTrigger
  AND (
    @IncludeDismissed = 1
   OR NOT EXISTS (SELECT 1
    FROM [dbo].[TaskExecutionMetadata] tem
    WHERE tem.[TaskExecutionId] = te.[Id]
  AND tem.[Key] = @DismissedKey)
    )
GROUP BY te.[Status];
