在查询设计中访问文本计数

Access text count in query design

我是 Access 的新手,我正在尝试开发一个查询,使我能够计算 table 具有 15 个字段的每个字段中一个词的出现次数。

table 只是存储员工的测试结果。有一个 table 存储员工标识 - id、姓名等。

第二个 table 有 15 个字段 - A1A15,每个字段中包含 correctincorrect。我需要每个字段的 incorrect 出现总数,而不是整个 table。

通过查询设计是否有答案,或者是否需要代码?

非常感谢解决方案,无论是查询设计还是代码!

首先,对于本应相对简单的请求,您努力获得预期结果的原因之一是因为您的数据不符合 database normalisation rules,因此,您是在违背自然规律查询数据时 RDBMS 的操作。

根据您的描述,我假设字段 A1A15 是测试问题的答案。

通过在您的数据库中将这些表示为单独的 字段 ,除了查询结果数据的固有困难(如您所发现的),如果您想要添加或删除一道题to/from测试,你将被迫重构你的整个数据库!

相反,我建议按以下方式构建您的 table:

结果

+------------+------------+-----------+
| EmployeeID | QuestionID |  Result   |
+------------+------------+-----------+
|          1 |          1 | correct   |
|          1 |          2 | incorrect |
|        ... |        ... | ...       |
|          1 |         15 | correct   |
|          2 |          1 | correct   |
|          2 |          2 | correct   |
|        ... |        ... | ...       |
+------------+------------+-----------+

这 table 将是您数据库中的 junction table(a.k.a。链接/交叉引用 table),支持 many- tables Employees & Questions 之间的对多关系,可能如下所示:

员工人数

+--------+-----------+-----------+------------+------------+-----+
| Emp_ID | Emp_FName | Emp_LName |  Emp_DOB   | Emp_Gender | ... |
+--------+-----------+-----------+------------+------------+-----+
|      1 | Joe       | Bloggs    | 01/01/1969 | M          | ... |
|    ... | ...       | ...       | ...        | ...        | ... |
+--------+-----------+-----------+------------+------------+-----+

问题

+-------+------------------------------------------------------------+--------+
| Qu_ID |                          Qu_Desc                           | Qu_Ans |
+-------+------------------------------------------------------------+--------+
|     1 | What is the meaning of life, the universe, and everything? |     42 |
|   ... | ...                                                        |    ... |
+-------+------------------------------------------------------------+--------+

使用这种结构,如果您想在测试中添加或删除问题,只需在 table 中添加或删除记录,而无需重新构建数据库 或重写任何依赖于现有结构的查询、表格或报告。

此外,由于答案的结果可能是二进制 correctincorrect,因此使用布尔值 True/False 表示会更好(并且效率更高) ] 数据类型,例如:

结果

+------------+------------+--------+
| EmployeeID | QuestionID | Result |
+------------+------------+--------+
|          1 |          1 | True   |
|          1 |          2 | False  |
|        ... |        ... | ...    |
|          1 |         15 | True   |
|          2 |          1 | True   |
|          2 |          2 | True   |
|        ... |        ... | ...    |
+------------+------------+--------+

这不仅会消耗更少的数据库内存,而且可以更有效地建立索引(产生更快的查询),并消除所有歧义和围绕拼写错误和区分大小写的错误的可能性。

使用这种新结构,如果您想查看每个员工的正确答案数,查询可以像这样简单:

select results.employeeid, count(*)
from results
where results.result = true
group by results.employeeid

或者,如果您想查看正确回答每个问题的员工人数(例如,要了解大多数员工答错了哪些问题),您可以使用如下内容:

select results.questionid, count(*)
from results
where results.result = true
group by results.questionid

以上显然是非常基本的示例查询,您可能希望将 Results table 加入 Employees table 和 Questions table 以获得有关结果的更丰富的信息。


将以上内容与您当前的数据库结构进行对比 -

根据你原来的问题:

The second table has 15 fields - A1 through A15 with the words correct or incorrect in each field. I need the total number of incorrect occurrences for each field, not for the entire table.

假设您要查看员工 的错误答案数量,您将不得不使用一个极其混乱的查询,例如:

select 
    employeeid,
    iif(A1='incorrect',1,0)+
    iif(A2='incorrect',1,0)+
    iif(A3='incorrect',1,0)+
    iif(A4='incorrect',1,0)+
    iif(A5='incorrect',1,0)+
    iif(A6='incorrect',1,0)+
    iif(A7='incorrect',1,0)+
    iif(A8='incorrect',1,0)+
    iif(A9='incorrect',1,0)+
    iif(A10='incorrect',1,0)+
    iif(A11='incorrect',1,0)+
    iif(A12='incorrect',1,0)+
    iif(A13='incorrect',1,0)+
    iif(A14='incorrect',1,0)+
    iif(A15='incorrect',1,0) as IncorrectAnswers
from
    YourTable

在这里,请注意答案编号也被硬编码到查询中,这意味着如果您决定添加一个新问题或删除一个现有问题,您不仅需要重组整个数据库,而且需要重新构建查询如上也需要重写