Sql 查询设计中的访问文本计数
我是刚接触Access的人,正在尝试开发一个查询,该查询允许我从一个包含15个字段的表中计算每个字段中出现一个单词的次数 该表仅存储员工的测试结果。有一个表存储员工标识-id、姓名等 第二个表有15个字段-Sql 查询设计中的访问文本计数,sql,ms-access,database-design,count,field,Sql,Ms Access,Database Design,Count,Field,我是刚接触Access的人,正在尝试开发一个查询,该查询允许我从一个包含15个字段的表中计算每个字段中出现一个单词的次数 该表仅存储员工的测试结果。有一个表存储员工标识-id、姓名等 第二个表有15个字段-A1到A15,每个字段中都有correct或correct。我需要每个字段的不正确的出现总数,而不是整个表 通过查询设计是否有答案,或者是否需要代码 无论是查询设计还是代码,我们都非常感谢您的解决方案 首先,对于一个相对简单的请求,您很难获得所需的结果,原因之一是您的数据不符合要求,因此,您在
A1
到A15
,每个字段中都有correct
或correct
。我需要每个字段的不正确的出现总数,而不是整个表
通过查询设计是否有答案,或者是否需要代码
无论是查询设计还是代码,我们都非常感谢您的解决方案 首先,对于一个相对简单的请求,您很难获得所需的结果,原因之一是您的数据不符合要求,因此,您在查询数据时违反了RDBMS的自然操作
根据您的描述,我假设A1
到A15
字段是测试问题的答案
通过在数据库中将这些字段表示为独立的字段,除了查询结果数据的固有困难(如您所发现的),如果您想在测试中添加或删除问题,您将被迫重新构建整个数据库
相反,我建议您按照以下方式构建表格:
结果
+------------+------------+-----------+
| EmployeeID | QuestionID | Result |
+------------+------------+-----------+
| 1 | 1 | correct |
| 1 | 2 | incorrect |
| ... | ... | ... |
| 1 | 15 | correct |
| 2 | 1 | correct |
| 2 | 2 | correct |
| ... | ... | ... |
+------------+------------+-----------+
+--------+-----------+-----------+------------+------------+-----+
| 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 |
| ... | ... | ... |
+-------+------------------------------------------------------------+--------+
+------------+------------+--------+
| EmployeeID | QuestionID | Result |
+------------+------------+--------+
| 1 | 1 | True |
| 1 | 2 | False |
| ... | ... | ... |
| 1 | 15 | True |
| 2 | 1 | True |
| 2 | 2 | True |
| ... | ... | ... |
+------------+------------+--------+
此表将是数据库中的一个(也称为链接/交叉引用表),支持表员工和问题之间的多对多关系,如下所示:
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
员工
+------------+------------+-----------+
| EmployeeID | QuestionID | Result |
+------------+------------+-----------+
| 1 | 1 | correct |
| 1 | 2 | incorrect |
| ... | ... | ... |
| 1 | 15 | correct |
| 2 | 1 | correct |
| 2 | 2 | correct |
| ... | ... | ... |
+------------+------------+-----------+
+--------+-----------+-----------+------------+------------+-----+
| 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 |
| ... | ... | ... |
+-------+------------------------------------------------------------+--------+
+------------+------------+--------+
| EmployeeID | QuestionID | Result |
+------------+------------+--------+
| 1 | 1 | True |
| 1 | 2 | False |
| ... | ... | ... |
| 1 | 15 | True |
| 2 | 1 | True |
| 2 | 2 | True |
| ... | ... | ... |
+------------+------------+--------+
问题
+------------+------------+-----------+
| EmployeeID | QuestionID | Result |
+------------+------------+-----------+
| 1 | 1 | correct |
| 1 | 2 | incorrect |
| ... | ... | ... |
| 1 | 15 | correct |
| 2 | 1 | correct |
| 2 | 2 | correct |
| ... | ... | ... |
+------------+------------+-----------+
+--------+-----------+-----------+------------+------------+-----+
| 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 |
| ... | ... | ... |
+-------+------------------------------------------------------------+--------+
+------------+------------+--------+
| EmployeeID | QuestionID | Result |
+------------+------------+--------+
| 1 | 1 | True |
| 1 | 2 | False |
| ... | ... | ... |
| 1 | 15 | True |
| 2 | 1 | True |
| 2 | 2 | True |
| ... | ... | ... |
+------------+------------+--------+
使用此结构,如果您希望在测试中添加或删除问题,您可以简单地从表中添加或删除记录,而无需重新构造数据库或重写任何依赖于现有结构的查询、表单或报告
此外,由于答案的结果可能是二进制的正确的
或不正确的
,因此使用布尔真/假数据类型表示这将更好(且效率更高),例如:
结果
+------------+------------+-----------+
| EmployeeID | QuestionID | Result |
+------------+------------+-----------+
| 1 | 1 | correct |
| 1 | 2 | incorrect |
| ... | ... | ... |
| 1 | 15 | correct |
| 2 | 1 | correct |
| 2 | 2 | correct |
| ... | ... | ... |
+------------+------------+-----------+
+--------+-----------+-----------+------------+------------+-----+
| 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 |
| ... | ... | ... |
+-------+------------------------------------------------------------+--------+
+------------+------------+--------+
| EmployeeID | QuestionID | Result |
+------------+------------+--------+
| 1 | 1 | True |
| 1 | 2 | False |
| ... | ... | ... |
| 1 | 15 | True |
| 2 | 1 | True |
| 2 | 2 | True |
| ... | ... | ... |
+------------+------------+--------+
这不仅可以减少数据库中的内存消耗,而且可以更高效地编制索引(生成更快的查询),并消除所有歧义和围绕打字错误和区分大小写的潜在错误
使用此新结构,如果您想查看每个员工的正确答案数量,查询可以简单到:
select results.employeeid,count(*)
根据结果
其中results.result=true
按结果分组。employeeid
或者,如果您想查看正确回答每个问题的员工人数(例如,为了了解大多数员工都错了哪些问题),您可以使用以下方法:
select results.questionid,count(*)
根据结果
其中results.result=true
按结果分组。问题ID
上述显然是非常基本的示例查询,您可能希望将结果
表连接到员工
表和问题
表中,以获得更丰富的结果信息
将上述内容与当前的数据库结构进行对比-
根据你原来的问题:
第二个表有15个字段-A1
到A15
,每个字段中都有correct
或correct
。我需要的是每个字段的错误出现总数,而不是整个表的错误出现总数
假设您想查看员工错误答案的数量,则必须使用令人难以置信的混乱查询,如以下所示:
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
这里,请注意,答案编号也硬编码到查询中,这意味着如果您决定添加新问题或删除现有问题,不仅需要重新构建整个数据库,还需要重写上述查询。我将其命名为[ms access]中的“每月答案”。)李,非常感谢!我希望得到一个答案;你给我上了一课!