rewrite main sql query
rewrite main sql query