Added count_by_sql function

This is necessary in order to be able to do things like
paginate_by_sql.