sql—仅基于范围的最小值的查找值

yhxst69z  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(399)

我想把值放在只给定最小值的范围内,类似于在excel中使用vlookup/hlookup使用“false”标准。
如下所示,tablescore列出了要指定特定点数(范围内的最小值)的值的低端切点(cutpointval)。下面的sql代码分两步完成,第一个查询生成一个数据表,每个低值包含一个高值,从而创建一个完整的范围。
然而,这是一种有点笨拙的方法,尤其是在尝试多次迭代时。原始表(tablescore)不能更改为包含高值。有没有办法只用一个查询来实现类似的机制?
主要的

ID              Score
72625           2.5
78261           3.2
82766           4.7
58383           0.3

表芯

CutpointVal     Points
0               0
0.3             1
1.2             2
2.7             3
3.4             4

上限和下限查询(rangeqry):

SELECT a.CutpointVal AS LowVal, Val(Nz((SELECT TOP 1 [CutpointVal]-0.001
FROM TableScore b
WHERE b.Points > a.Points
ORDER BY b.Points ASC),9999)) AS HighVal, a.Points
FROM TableScore AS a
ORDER BY a.Points;

范围分配查询:

SELECT Main.ID, Main.Score, RangeQry.LowVal, RangeQry.HighVal, RangeQry.Points AS PTS
FROM RangeQry, Main
WHERE (((Main.Score) Between [RangeQry].[LowVal] And [RangeQry].[HighVal]));

期望输出:

ID        Score        Points
72625     2.5          2
78261     3.2          3
82766     4.7          4
58383     0.3          1
1szpjjfi

1szpjjfi1#

考虑:

SELECT Main.ID, Main.Score, (
    SELECT Max(Points) FROM TableScore WHERE CutpointVal<=Main.Score) AS Pts
FROM Main;

SELECT Main.ID, Main.Score, (
    SELECT TOP 1 Points FROM TableScore 
    WHERE CutpointVal <= Main.Score 
    ORDER BY Points DESC) AS Pts
FROM Main;

SELECT Main.ID, Main.Score, DMax("Points","TableScore","CutpointVal<=" & [Score]) AS Pts
FROM Main;

相关问题