remove use of DISTINCT in sql query