[英]SQL - Retrieve Closest Lower Value
When a column value does not equal, I would like to retrieve the closest lower pay value. 当列值不相等时,我想检索最接近的较低薪水值。
For instance : 10 yearsOfService should equal the value 650.00; 例如 :10 yearsOfService应该等于值650.00; 14 yearsOfService would equal the value 840.00 in the below incentive table, 14 yearsOfService等于下面的激励表中的值840.00,
ID Pay yearsOfService
1 125.00 0
2 156.00 2
3 188.00 3
4 206.00 4
5 650.00 6
6 840.00 14
7 585.00 22
8 495.00 23
9 385.00 24
10 250.00 25
I have tried several different approaches; 我尝试了几种不同的方法。 including: 包含:
SELECT TOP 1 (pay) as incentivePay
FROM incentive
WHERE yearsOfService = '10'
This works but only for yearsOfService that match. 这有效,但仅适用于匹配的YearsOfService。
With 10 yearsOfService: 拥有10年服务:
RESULTSET = [1 650.00]
Any ideas? 有任何想法吗?
Please try: 请试试:
SELECT TOP 1 (pay) as incentivePay
FROM incentive
WHERE yearsOfService <= '10'
ORDER BY yearsOfService desc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.