public function uniqueForTrackId($trackId, $respTrackId, $previousAppIds)
    {
        if ($previousAppIds) {
            // When unique for all tracks of this type the current track
            // appointment id's should also be excluded.
            $this->uniqueInTrackInstance($previousAppIds);
        }

        $sql = "gap_id_appointment NOT IN
                    (SELECT gr2t2a_id_appointment FROM gems__respondent2track2appointment
                        INNER JOIN gems__respondent2track
                            ON gr2t2a_id_respondent_track = gr2t_id_respondent_track
                        WHERE gr2t2a_id_appointment IS NOT NULL AND
                            gr2t_id_track = $trackId";

        if ($respTrackId) {
            // Exclude all fields of the current respondent track as it is being recalculated
            // and therefore they may have changed.
            //
            // Instead we filter here on $previousAppIds
            $sql .= " AND NOT (gr2t2a_id_respondent_track = $respTrackId)";
        }

        $sql .= ")";

        $this->_select->where($sql);
    }