fixed sql query

1. fixed duplication of data
2. added a group by clause
3. all data MUST be recorder in the categories link table essentially