基于查找表的SQL线性插值
我需要使用包含查找值的连接表(实际上更像查找阈值)将线性插值构建到 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您想要
Main_Value
和最接近的(低而不是高)或相等的Normalized_Value
,您可以这样做:替换
#t1
和#t2
通过正确的表名。Assuming you want the
Main_Value
and the nearest (low and not high) or equalNormalized_Value
, you can do it like this:Replace
#t1
and#t2
by the correct tablenames.