changed to working db scheme
changed getIntervalQuery so it can be used for multiple calculation times