首页 > 解决方案 > 如何在 where 子句中使用多行?

问题描述

TRAVEL    |  SIZE
0         |  0.41
2.5       |  0.45
5.0       |  0.50
7.5       |  0.54
10        |  0.58

我正在使用 Firebird 数据库。最终目标是根据给定的大小获得旅行。我面临的问题是给定的大小是连续的。

例如,给定大小为 0.47。然后,从插值公式得到旅行:(0.47-0.45)*(5.0-2.5)/(0.5-0.45)+2.5 = 3.5

实际上,在提取给定大小周围的 2 行之后,我尝试在 C# 中进行计算。但我被困在提取 2 行。

我该如何解决这个问题。

标签: c#sqlwhere-clausefirebird

解决方案


lead()为此,我建议:

select (case when 0.47 = size then travel
             else travel + (next_travel - travel) * (0.47 - size) / (next_size - size)
        end) as imputed_size
from (select t.*,
             lead(size) over (order by size) as next_size,
             lead(travel) over (order by size) as next_travel
      from t
     ) t
where 0.47 >= size and
      (0.47 < next_size or next_size is null);

在旧版本的 Firebird 中,您可以使用相关子查询来获取下一个值。


推荐阅读