changed the query to only return distinct and order by pid