refactor day count query