implement record group and having query