Reduce number of queries necessary for getting values for a period