Solved PG distinct and order_by issue