我有一个有两个维度的运费表:
Zone
和Weight
。
Zone
是一个精确值。 Weight
是给定值较低(含)的范围。这意味着在给定的示例中,最高范围是 200-∞。
我正在寻找给定
Zone
和 Weight
的成本。
SELECT TOP 1 *
FROM fg.CostTest
WHERE Zone = 'A' AND Weight <= 123
ORDER BY Weight DESC
表中有几十万行,在性能方面有没有更好的方法来实现这一点?
我正在使用 SQL Server 2022。
这可能有帮助:
select * from (
select *, row_number() over (order by abs(weight - 123)) rn
where zone = 'A'
) t where rn = 1