我有一个具有以下结构的表。
从该表中,我需要使用表中给出的范围找到价格。
ex:
if i give the Footage_Range1=100 means it will give the output as 0.00 and
if Footage_Range1=101 means the output is 2.66
if Footage_Range1=498 means the output is 2.66
如何编写查询以获取价格?
如果我正确理解了您的要求,您可以试试这个:
SELECT
price
FROM
my_table
WHERE
Footage_Range1 <= YOUR_RANGE
ORDER BY
Footage_Range1 DESC
LIMIT 1
输入在哪里YOUR_RANGE
:100,101,498 等
基本上,此查询将返回最接近Footage_Range1
小于或等于输入的价格。
我有你要求的样品。请看一下。
DECLARE @range INT = 498
DECLARE @Test TABLE(mfg_id INT, footage_range INT, price FLOAT)
INSERT INTO @Test ( mfg_id, footage_range, price )
SELECT 2, 0, 0.00
UNION ALL SELECT 2, 101, 2.66
UNION ALL SELECT 2, 500, 2.34
UNION ALL SELECT 2, 641, 2.21
UNION ALL SELECT 2, 800, 2.11
UNION ALL SELECT 2, 1250, 2.06
SELECT TOP 1
*
FROM @Test WHERE footage_range <= @range
ORDER BY footage_range DESC
try below code..
select price from your_table where footage_range1 <=rangevalue limit 1;