SQL - Find the closest price to a given argument
I am trying to find the tuples which price is closest to a given parameter in SQL. Forexample: the parameter price = 6 would return id 1 and 2. Parameter price = 20 would开发者_JAVA技巧 return id 3.
Table :
ID PRICE
1 5
2 5
3 10
SELECT id
FROM Table
WHERE table.price ?? 6
Any Ideas how to do this?
Thank you.
This query selects the closest price using the subquery, then returns all rows which match that price:
SELECT *
FROM Table
WHERE price = ( --could also use "WHERE price in" here...
SELECT TOP 1 price FROM Table
GROUP BY price
ORDER BY Min(Abs(price - ?)))
SELECT ID
FROM TableX
WHERE PRICE = @CheckPrice +
( SELECT MIN(ABS(PRICE - @CheckPrice))
FROM TableX
)
OR PRICE = @CheckPrice -
( SELECT MIN(ABS(PRICE - @CheckPrice))
FROM TableX
)
or (probably better for performance):
SELECT ID
FROM
TableX
CROSS JOIN
( SELECT
( SELECT MIN(PRICE) - @CheckPrice
FROM TableX
WHERE PRICE >= @CheckPrice
) AS Pover
, ( SELECT @CheckPrice - MAX(PRICE)
FROM TableX
WHERE PRICE <= @CheckPrice
) AS Punder
FROM dual
) AS tmp
WHERE PRICE IN ( @CheckPrice + LEAST(Pover, Punder)
, @CheckPrice - LEAST(Pover, Punder)
)
SELECT id, price
FROM thetable t1
WHERE NOT EXISTS ( SELECT *
FROM thetable t2
WHERE abs (t2.price - 6) < abs (t1.price - 6)
);
We are using UNION subquery to get closest prices for target price. In each subquery we get absolute delta (diff between each model price and target price) and ORDER BY delta in the end. Works pretty fast and stable. Hope it helps ;)
SET @CheckPrice = 3910;
(
SELECT
m_id,
m_name,
m_auto_price,
m_auto_discount,
ABS(m_auto_price - @CheckPrice)AS delta
FROM
s_models
WHERE
m_auto_price >= @CheckPrice
ORDER BY
m_auto_price ASC
LIMIT 10
)
UNION
(
SELECT
m_id,
m_name,
m_auto_price,
m_auto_discount,
ABS(m_auto_price - @CheckPrice)AS delta
FROM
s_models
WHERE
m_auto_price <= @CheckPrice
ORDER BY
m_auto_price DESC
LIMIT 10
)
ORDER BY
delta ASC,
m_auto_price DESC,
m_auto_discount DESC
精彩评论