我在 MS Access 中有一个包含调查结果的数据表,并且我有一个基于调查结果的风险 ID 和风险描述的查找表。
到目前为止,我尝试的是从我的调查表中选择不同的条目,并在我的风险代码查询中输入一个新字段,其数量取决于我确定的标准,然后我将使用它来查找风险。
我的调查表如下所示:
Name | Location | Days spent eating IceCream | Icecream eating location
John Smith | London | 30 | Hull
我的风险 ID 表如下所示:
RiskID | RiskBool | Description
1 | Yes | At risk - This person eats too much icecream
2 | Yes | Risk - This person does not eat enough icecream
3 | No | Sensible amount of icecream eaten
4 | Yes | It is illegal to eat icecream in Hull
我的查询在访问设计视图中看起来像这样
Name | Location | Risk Code | RiskID | Description
我想编写 SQL将风险代码更改为 1、2、3、4(在我的实际案例中最多为15),然后我会告诉它只显示风险 ID 和代码匹配时的人员和描述。我还没有写这个。
实现这一目标的最佳方法是什么?
我看到两种可能性:
- 为每个风险 ID设置15 个查询,将描述添加到这些查询中,然后将这 15 组结果连接在一起。这是我知道该怎么做,但最终可能会很混乱。
- 使用语句设置一些“检查”
if
,然后设置该条目的风险代码字段。
我当前的 SQL 看起来像这样,但它还没有进行任何检查,我担心 if 语句会非常非常长。
SELECT DISTINCT
[At Risk Employee List].Employee AS Name,
[At Risk Employee List].[DaysIceCream] AS [Days spent eating Icecream],
[At Risk Employee List].[Base Location],
[RiskCode] AS [Risk Code], <----is this where the check would need to go?
RiskDescLookup.RiskBoolean,
RiskDescLookup.RiskExplanation
FROM RiskDescLookup,
[Survey Raw Data]
INNER JOIN
[At Risk Employee List]
ON
[Survey Raw Data].ResID = [At Risk Employee List].[Staff ID]
GROUP BY
[At Risk Employee List].Employee,
[At Risk Employee List].[DaysIceCream],
[At Risk Employee List].[Base Location],
RiskDescLookup.RiskID,
[RiskCode] AS [Risk Code], <----is this where the check would need to go?
RiskDescLookup.RiskBoolean,
RiskDescLookup.RiskExplanation
我想象语句所做的检查if
很长,看起来像(在伪代码中):
if ( [At Risk Employee List].[Base Location] = Hull, then [RiskCode]=4...., else if (DaysIceCream>42) then....
这是最好的方法吗?我什至需要有风险代码吗?
对于如何以最佳方式生成此“检查”,我有点迷茫。