Fix for error when aggregate used in having predicate