我有一张具有以下结构的表格。
从该表中我需要使用表中给出的范围来查找价格。
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
如何编写查询以获取价格?
答案 0 :(得分:6)
如果我理解你的要求你可以试试这个:
SELECT
price
FROM
my_table
WHERE
Footage_Range1 <= YOUR_RANGE
ORDER BY
Footage_Range1 DESC
LIMIT 1
YOUR_RANGE
是输入:100,101,498等
此查询基本上会将最接近的价格返回到Footage_Range1
输入的较小或相等。
答案 1 :(得分:1)
我有满足您要求的样本。请看一下。
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
答案 2 :(得分:0)
try below code..
select price from your_table where footage_range1 <=rangevalue limit 1;