fixing search sql query for grouping