minor fixes to the sql query for stats retrieval