SELECT COUNT(*)
FROM   (SELECT P.person_id AS personId,
               dbo.DATETIME_GREATEST_2PARAM(P.modified_at, UGM.modified_at) AS lastModified
        FROM   user_group_membership UGM
               LEFT JOIN person P
                      ON UGM.member = P.person_id
        WHERE  UGM.user_group_membership_id IN (SELECT
               Max(UGM.user_group_membership_id)
                                                FROM   user_group_membership UGM
                                                       LEFT JOIN person P
                                                              ON UGM.member =
                                                                 P.person_id
                                                WHERE  P.customer_id = :customerparam
                                                       AND UGM.customer_id = :customerparam
                                                       AND UGM.assigned_group_id = :groupparam
                                                       AND (
                       UGM.modified_at >= :update_after
                        OR P.modified_at >= :update_after )
                                                GROUP  BY UGM.member)) A
WHERE  lastmodified > :update_after
        OR ( lastmodified = :update_after
             AND personId > :person_id )