Return MS Access 范围内的值

Return a value from a range in MS Access

我正在尝试在 MS Access 中进行非常简单的范围查找。

我基本上有两个表:

我正在寻找能够找到合适的薪水范围和 return 正确税率的查询。

我以此为基础开始:

SELECT IIf(Salary Between 1000 And 2000,10,20)
FROM Employees;

但我没有取得任何进展。

如有任何帮助,我们将不胜感激。

一种可能的方法是使用相关子查询,例如:

select e.*, (select top 1 t.taxrate from taxrates t where t.to > e.salary order by t.to)
from employees e

或使用between:

select e.*, (select top 1 t.taxrate from taxrates t where e.salary between t.to and t.from)
from employees e

或者,您可以按以下方式使用 left join

select e.*, t.taxrate
from employees e left join taxrates t on (e.salary between t.to and t.from)

请注意,MS Access 无法在查询设计视图中表示这种类型的联接(即具有 'calculated' 联接标准的联接,而不是基于相等字段值的联接),但这仍然有效 SQL 可能会被 JET 数据库引擎成功评估。


在以上所有内容中,我假设您的表格名为 EmployeesTaxRates,请根据需要更改它们。

这是一个替代解决方案:

SELECT Salary.*, TaxRate.TaxRate
FROM Salary, TaxRate
WHERE Salary BETWEEN From and TO;