'Return two possibly None nodes: one for those parts of self that
should be included in the WHERE clause and one for those parts of
self that must be included in the HAVING clause.'

def split_having(self, negated=False):

    if (not self.contains_aggregate):
        return (self, None)
    in_negated = (negated ^ self.negated)
    may_need_split = ((in_negated and (self.connector == AND)) or ((not in_negated) and (self.connector == OR)))
    if (may_need_split and self.contains_aggregate):
        return (None, self)
    where_parts = []
    having_parts = []
    for c in self.children:
        if hasattr(c, 'split_having'):
            (where_part, having_part) = c.split_having(in_negated)
            if (where_part is not None):
                where_parts.append(where_part)
            if (having_part is not None):
                having_parts.append(having_part)
        elif c.contains_aggregate:
            having_parts.append(c)
        else:
            where_parts.append(c)
    having_node = (self.__class__(having_parts, self.connector, self.negated) if having_parts else None)
    where_node = (self.__class__(where_parts, self.connector, self.negated) if where_parts else None)
    return (where_node, having_node)
