dynamic sql to calculate resource counts for each daily. udpated tests.