Select and group records by time intervals