基于查找表的SQL线性插值

发布于 2025-01-08 10:09:27 字数 2038 浏览 3 评论 0原文

我需要使用包含查找值的连接表(实际上更像查找阈值)将线性插值构建到 SQL 查询中。由于我对 SQL 脚本编写相对较新,因此我搜索了示例代码来为我指明正确的方向,但我遇到的大多数 SQL 脚本都是用于在日期和时间戳之间进行插值,我无法将它们与我的情况联系起来。

基本上,我有一个主数据表,在单列中包含许多行十进制值,例如:

Main_Value
0.33
0.12
0.56
0.42
0.1  

现在,我需要基于包含 6 行的联接查找表,为上面的每一行生成插值数据点,其中包含非-线性阈值和关联的线性归一化值:

Threshold_Level     Normalized_Value
0                            0
0.15                         20
0.45                         40
0.60                         60
0.85                         80
1                           100

例如,如果 Main_Value 列中的值为 0.45,则查询将在最近的 Threshold_Level 中(或之间)查找其位置,并根据中的相邻值对其进行插值Normalized_Value 列(在此示例中将生成值 40)。

我真的很感激任何关于围绕此构建 SQL 查询的见解,特别是因为很难追踪使用连接表的线性插值的任何 SQL 示例。

有人指出我可以使用某种舍入,因此我在下面提供了更详细的表格。我希望 SQL 查询查找位于下表中 Threshold_Min 和 Threshold_Max 值之间的每个 Main_Value(来自上面的第一个表),并返回“Normalized_%”值:

    Threshold_Min   Threshold_Max   Normalized_%
    0.00                0.15             0
    0.15                0.18             5
    0.18                0.22             10
    0.22                0.25             15
    0.25                0.28             20
    0.28                0.32             25
    0.32                0.35             30
    0.35                0.38             35
    0.38                0.42             40
    0.42                0.45             45
    0.45                0.60             50
    0.60                0.63             55
    0.63                0.66             60
    0.66                0.68             65
    0.68                0.71             70
    0.71                0.74             75
    0.74                0.77             80
    0.77                0.79             85
    0.79                0.82             90
    0.82                0.85             95
    0.85                1.00             100

例如,如果 Main_Value 表中的值为 0.52,它介于 Threshold_Min 0.45 和 Threshold_Max 0.60 之间,因此返回的 Normalized_% 为 50%。问题在于 Threshold_Min 和 Max 值不是线性的。有人能指出我如何编写这个脚本的方向吗?

I need to build linear interpolation into an SQL query, using a joined table containing lookup values (more like lookup thresholds, in fact). As I am relatively new to SQL scripting, I have searched for an example code to point me in the right direction, but most of the SQL scripts I came across were for interpolating between dates and timestamps and I couldn't relate these to my situation.

Basically, I have a main data table with many rows of decimal values in a single column, for example:

Main_Value
0.33
0.12
0.56
0.42
0.1  

Now, I need to yield interpolated data points for each of the rows above, based on a joined lookup table with 6 rows, containing non-linear threshold values and the associated linear normalized values:

Threshold_Level     Normalized_Value
0                            0
0.15                         20
0.45                         40
0.60                         60
0.85                         80
1                           100

So for example, if the value in the Main_Value column is 0.45, the query will lookup its position in (or between) the nearest Threshold_Level, and interpolate this based on the adjacent value in the Normalized_Value column (which would yield a value of 40 in this example).

I really would be grateful for any insight into building a SQL query around this, especially as it has been hard to track down any SQL examples of linear interpolation using a joined table.

It has been pointed out that I could use some sort of rounding, so I have included a more detailed table below. I would like the SQL query to lookup each Main_Value (from the first table above) where it falls between the Threshold_Min and Threshold_Max values in the table below, and return the 'Normalized_%' value:

    Threshold_Min   Threshold_Max   Normalized_%
    0.00                0.15             0
    0.15                0.18             5
    0.18                0.22             10
    0.22                0.25             15
    0.25                0.28             20
    0.28                0.32             25
    0.32                0.35             30
    0.35                0.38             35
    0.38                0.42             40
    0.42                0.45             45
    0.45                0.60             50
    0.60                0.63             55
    0.63                0.66             60
    0.66                0.68             65
    0.68                0.71             70
    0.71                0.74             75
    0.74                0.77             80
    0.77                0.79             85
    0.79                0.82             90
    0.82                0.85             95
    0.85                1.00             100

For example, if the value from the Main_Value table is 0.52, it falls between Threshold_Min 0.45 and Threshold_Max 0.60, so the Normalized_% returned is 50%. The problem is that the Threshold_Min and Max values are not linear. Could anyone point me in the direction of how to script this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

另类 2025-01-15 10:09:27

假设您想要 Main_Value 和最接近的(低而不是高)或相等的 Normalized_Value,您可以这样做:

select t1.Main_Value, max(t2.Normalized_Value) as Normalized_Value
from #t1 t1
inner join #t2 t2 on t1.Main_Value >= t2.Threshold_Level
group by t1.Main_Value

替换 #t1#t2 通过正确的表名。

Assuming you want the Main_Value and the nearest (low and not high) or equal Normalized_Value, you can do it like this:

select t1.Main_Value, max(t2.Normalized_Value) as Normalized_Value
from #t1 t1
inner join #t2 t2 on t1.Main_Value >= t2.Threshold_Level
group by t1.Main_Value

Replace #t1 and #t2 by the correct tablenames.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文