Sql Window Functions Lead Lag Thermostat