Modify SQL to get ranquing