fix broken DB query for calculating peak and avg values