Sql 从两个最接近的值进行线性插值就足够了吗?你会寻找13.2的答案吗选择((166.0-150)/(175.0-150))*(15.0-10)+10-1如果容量降低不是温度升高的函数,则此操作将失败。啊,你在回答中说,好吧,不是-1,只是-1/2。@yperc

Sql 从两个最接近的值进行线性插值就足够了吗?你会寻找13.2的答案吗选择((166.0-150)/(175.0-150))*(15.0-10)+10-1如果容量降低不是温度升高的函数,则此操作将失败。啊,你在回答中说,好吧,不是-1,只是-1/2。@yperc,sql,math,interpolation,Sql,Math,Interpolation,从两个最接近的值进行线性插值就足够了吗?你会寻找13.2的答案吗选择((166.0-150)/(175.0-150))*(15.0-10)+10-1如果容量降低不是温度升高的函数,则此操作将失败。啊,你在回答中说,好吧,不是-1,只是-1/2。@ypercube:我会在心里从自己身上减去一分;)我将把这个评论改为粗体。非常非常感谢。减少容量是温度的增加函数。-1如果减少容量不是温度的增加函数,这将失败。啊,你在回答中说,好吧,不是-1,只是-1/2。@ypercube:我会在心里从自己身上减去一


从两个最接近的值进行线性插值就足够了吗?你会寻找13.2的答案吗<代码>选择((166.0-150)/(175.0-150))*(15.0-10)+10-1如果容量降低不是温度升高的函数,则此操作将失败。啊,你在回答中说,好吧,不是-1,只是-1/2。@ypercube:我会在心里从自己身上减去一分;)我将把这个评论改为粗体。非常非常感谢。减少容量是温度的增加函数。-1如果减少容量不是温度的增加函数,这将失败。啊,你在回答中说,好吧,不是-1,只是-1/2。@ypercube:我会在心里从自己身上减去一分;)我将把那个评论改为粗体。非常非常感谢。降低容量就是提高温度的作用。
temperature  decrease_capacity
----------    ----------------
125           5
150           10
175           15
c1 + (t - t1) / (t2 - t1) * (c2 - c1)

t  = input tempurate
t1 = nearest lower temperature
t2 = nearest upper temperature
c1 = capacity belonging to t1
c2 = capacity belonging to t2
declare @YourTable table (temperature int, decrease_capacity int)
insert @YourTable values (125, 5), (150, 10), (175, 15)

declare @temp int
set @temp = 166

select  case 
        when below.temperature is null then above.decrease_capacity
        when above.temperature is null then below.decrease_capacity
        else below.decrease_capacity + 1.0 *
             (@temp - below.temperature) / 
             (above.temperature - below.temperature) *
             (above.decrease_capacity - below.decrease_capacity)
        end
from    (
        select  min(temperature) as mintemp
        from    @YourTable 
        where   temperature >= @temp
        ) abovetemp
left join
        @YourTable above
on      above.temperature = abovetemp.mintemp
cross join
        (
        select  max(temperature) as maxtemp
        from    @YourTable 
        where   temperature < @temp
        ) belowtemp
left join
        @YourTable below
on      below.temperature = belowtemp.maxtemp
select (select max(dc) from test where t <= 166) + 
       (166 - (select max(t) from test where t <= 166)) * 
       ((select min(dc) from test where t >= 166) - 
        (select max(dc) from test where t <= 166)) / 
       ((select min(t) from test where t >= 166) - 
        (select max(t) from test where t <= 166))
DECLARE @SearchTemp FLOAT = 166;

WITH T(temperature, decrease_capacity) AS
(
SELECT 125.0,5.0 UNION ALL
SELECT 150.0,10 UNION ALL
SELECT 175.0,15 
), T2 AS
(
SELECT TOP 1 'L' as bound, temperature, decrease_capacity
FROM T 
WHERE temperature <= @SearchTemp
ORDER BY temperature DESC
UNION ALL
SELECT TOP 1 'U' as bound,  temperature, decrease_capacity
FROM T 
WHERE temperature >= @SearchTemp
ORDER BY temperature 
)
SELECT CASE
         WHEN COUNT(*) = 2 THEN CASE
                                  WHEN COUNT(DISTINCT temperature) = 1 THEN MAX(decrease_capacity)
                                  ELSE ((@SearchTemp-MAX(CASE WHEN bound = 'L' THEN temperature END) )/(MAX(CASE WHEN bound = 'U' THEN temperature END) -MAX(CASE WHEN bound = 'L' THEN temperature END) )) * (MAX(CASE WHEN bound = 'U' THEN decrease_capacity END)-MAX(CASE WHEN bound = 'L' THEN decrease_capacity END)) + MAX(CASE WHEN bound = 'L' THEN decrease_capacity END)
                                END
       END
FROM   T2