refactor sql queries, bug getAllStat