从Microsoft Access中的数字值计算字母等级

时间:2018-01-11 17:20:15

标签: sql ms-access

我在MS Access中有一个专栏,其中包含学生的课程成绩信息。我正在尝试根据成绩列的内容计算另一栏中的字母等级。

以下是我到目前为止:

IIf([Grade]>92.5,"A",

IIf([Grade]>89.5,IIf([Grade]<92.5,"A-",

IIf([Grade]>86.5,IIf([Grade]<89.5,"B+",

IIf([Grade]>82.5,IIf([Grade]<86.5,"B",

IIf([Grade]>79.5,IIf([Grade]<82.5,"B",

IIf([Grade]>69.5,IIf([Grade]<79.5,"C",

IIf([Grade]<69.5,"F"))))))))))))

这适用于&#34; A&#34;和&#34; A - &#34;但所有其他课程成绩都是空白。

这是&#34; A&#34;和&#34; A - &#34;独自工作:

IIf([Grade]>92.5,"A",IIf([Grade]>89.5,IIf([Grade]<92.5,"A-")))

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

可能是使用Switch更简单,例如

SELECT
    Switch(
        [Grade]>92.5,"A",
        [Grade]>=89.5, "A-",
        [Grade]>=86.5, "B+",
        [Grade]>=82.5, "B",
        [Grade]>=79.5, "B",
        [Grade]>=69.5, "C",
        [Grade]<69.5, "F") AS my_grade
FROM my_table 

答案 1 :(得分:1)

您的直接问题是,在您的嵌套IIF中,您首先放置低端,然后放置高端秒。只需反向输出所有匹配的字母,但错过0.5个端点。

IIf([Grade]>92.5,"A",

IIf([Grade]<92.5, IIf([Grade]>89.5,"A-",

IIf([Grade]<89.5, IIf([Grade]>86.5,"B+",

IIf([Grade]<86.5, IIf([Grade]>82.5,"B",

IIf([Grade]<82.5, IIf([Grade]>79.5,"B-",

IIf([Grade]<79.5, IIf([Grade]>69.5,"C",

IIf([Grade]<69.5,"F")))))))))))) As Letter

或者,使用IIF BETWEEN> AND <的人数较少BETWEEN。由于.49999999包含两个终点并且详尽无遗,因此请考虑添加IIf([Grade]>92.5,"A", IIf([Grade] BETWEEN 89.5 AND 92.49999999,"A-", IIf([Grade] BETWEEN 86.5 AND 89.49999999,"B+", IIf([Grade] BETWEEN 82.5 AND 86.49999999,"B", IIf([Grade] BETWEEN 79.5 AND 82.49999999,"B-", IIf([Grade] BETWEEN 69.5 AND 79.49999999,"C", IIf([Grade] <= 69.49999999,"F", NULL))))))) As Letter2,

>

但即使这样,只需要IIf([Grade]>92.5,"A", IIf([Grade] > 89.5,"A-", IIf([Grade] > 86.5,"B+", IIf([Grade] > 82.5,"B", IIf([Grade] > 79.5,"B-", IIf([Grade] > 69.5,"C", IIf([Grade]<=69.5,"F", NULL))))))) As Letter3 的高点,因为你将所有条件嵌套在一起。

JOIN

但是,正如@MatBailie建议的那样,考虑一个查找表并运行一个范围连接查询(使用逗号分隔的表而不包含任何ID Letter MinValue MaxValue 1 A 92.5 1000 2 A- 89.5 92.4999999 3 B+ 86.5 89.4999999 4 B 82.5 86.4999999 5 B- 79.5 82.4999999 6 C 69.5 79.4999999 7 F -1000 69.4999999 ),然后按值过滤。考虑甚至加入100以上(额外信用)和零以下,有空间实际达到0.5个端点。如果您调整成绩点,这也可以提供更好的维护。

GradeLetter

SELECT l.Letter, g.Grade
FROM Grades g, GradeLetter l
WHERE g.Grade 
BETWEEN l.MinValue AND l.MaxValue;

范围加入查询

keytool -list -v -keystore [keystore_path] -storepass [password_here]