我想产生结果:
Table: HWData
Effective Date: Year Built: Home Age: Home Age Factor:
10/8/2011 1980 31 1.1
根据以下规则:
New Construction 0.800
1 to 5 years old 0.850
6 to 10 years old 0.900
11 to 15 years old 0.950
16 to 20 years old 1.000
21 to 30 years old 1.050
31 to 40 years old 1.100
over 40 years old 1.150
这就是我所拥有的:
Select HWData.[Effective Date],
HWData.[Year Built],
EXTRACT(YEAR FROM [Effective Date]) - HWData.[Year Built] AS Home Age,
iif(HWData.[Home Age] = 0, 0.8,
iif(HWData.[Home Age] <= 5, 0.85,
iif(HWData.[Home Age] <= 10, 0.9,
iif(HWData.[Home Age] <= 15, 0.95,
iif(HWData.[Home Age] <= 20, 1,
iif(HWData.[Home Age] <= 30, 1.05,
iif(HWData.[Home Age] <= 40, 1.1,
iif(HWData.[Home Age] > 40, 1.15,
0)))))))) AS [Home Age Factor]
FROM HWData
INNER JOIN Factor_HomeAge
ON HWData.[Policy Number] = Factor_HomeAge.[Policy Number]
但是整个sql代码都不会运行。有没有直接的方式来获得家庭年?
我认为内连接也是错误的,因为没有列名称Factor_HomeAge。[政策号]。
答案 0 :(得分:1)
而不是EXTRACT(YEAR FROM [Effective Date])
Access具有DATEPART
功能。您将需要使用:
DATEPART("YYYY", [Effective Date])
我还注意到您的第一个IIF()
声明:iif(HWData.[Home Age] = 0, 0.8,
如果您允许家庭年龄的部分年份值,您可能需要将查询更改为:
iif(HWData.[Home Age] < 1, 0.8,
来计算不到一年的房屋。