fix statistics sql bugs