some refactoring and group by table begin