Fix sql for purchase stats