SELECT COUNT(*)
FROM   (SELECT P.person_id AS personId,
               GREATEST(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 >= TO_DATE(:update_after,'yyyy-mm-dd hh24:mi:ss')
                        OR P.modified_at >= TO_DATE(:update_after,'yyyy-mm-dd hh24:mi:ss'))
                                                GROUP  BY UGM."member")) A
WHERE  lastmodified > TO_DATE(:update_after,'yyyy-mm-dd hh24:mi:ss')
        OR ( lastmodified = TO_DATE(:update_after,'yyyy-mm-dd hh24:mi:ss')
             AND personId > :person_id )
