简体   繁体   English

MS-Access-唯一记录

[英]MS-Access - unique records

I have a dataset with a lot of students, each with their own student number. 我有一个包含很多学生的数据集,每个学生都有自己的学生编号。 Each student has done multiple quizzes. 每个学生都进行了多次测验。 They have often done a quiz more than once. 他们经常不止一次进行测验。 I can easily find out how many quizzes in total each student has completed but I'd also like to find out how many different unique quizzes each student has done. 我可以轻松地找出每个学生总共完成了多少个测验,但我也想找出每个学生完成了多少个不同的独特测验。 For example, a student may have completed 6 quizzes, but they have only completed 3 unique quizzes (maybe they did each unique quiz twice). 例如,一个学生可能已经完成了6次测验,但他们仅完成了3次独特测验(也许他们对每个独特测验进行了两次)。 How can I do this in Access? 如何在Access中做到这一点?

You want to use the DISTINCT predicate in your SQL query. 您想在SQL查询中使用DISTINCT谓词。 Since I don't know what your table looks like, I'm just going to give you an example: 由于我不知道您的桌子是什么样子,所以我仅举一个例子:

SELECT DISTINCT NAME_OF_QUIZ FROM MYTABLE WHERE STUDENTID=?
SELECT DISTINCT tbl_Quizzes.Student, tbl_Quizzes.Quiz
FROM tbl_Quizzes;

Or go into the Query design window, right click on it and choose Properties, Unique Values, Yes. 或者进入“查询设计”窗口,右键单击它,然后选择“属性”,“唯一值”,“是”。

It would really help to have the tables names and column names so the answers can be fined tuned to your exact setup easier. 拥有表名和列名将确实有帮助,因此可以轻松地将答案调整为更精确的设置。

For the sake of this example, we will assume you have one table, called Table1 and it has columns Student and Quiz , quiz being the name (unique identifier) of the quiz that was taken. 在本例中,我们假设您有一个表,称为Table1,表中有StudentQuiz列,quiz是所进行的测验的名称(唯一标识符)。

An sql to get the count of unique quizzes a student took, would look like this: 一个用于获取学生进行的独特测验次数的sql看起来像这样:

Select a.Student, count(a.Quiz) as QuizCount
From (Select Distinct Student, Quiz from Table1) as a
Group by a.Student

The (Select Distinct Student, Quiz from Table1) is in this case called a sub-query, which means that it is a query inside a query. 在这种情况下(Select Distinct Student, Quiz from Table1)称为子查询,这意味着它是查询内部的查询。 The purpose of this sub-query is to get a resultant table that you can then get a count from that only has unique quizzes that a student took which is accomplished by the keyword Distinct. 此子查询的目的是获得一个结果表,然后您可以从中得到一个计数,该计数仅具有学生通过关键字Distinct完成的唯一测验。

The as a and as QuizCount is called aliasing. as aas QuizCount称为别名。 Tables and columns can be aliased and often are for purposes such as not having to repeatedly type out a long table name, to increase readability of the query, to name resultant column of an aggregate function such as count, avg, sum, etc. since if no alias is provided the resultant column name of an aggregate function is something like EXR1000. 可以对表和列进行别名处理,并且通常是出于以下目的:不必重复键入较长的表名;可以提高查询的可读性;可以为聚合函数(例如count,avg,sum等)命名结果列,因为如果未提供别名,则聚合函数的列名称将类似于EXR1000。

Lastly, any column in your result set that is not a result of an aggregate function, such as a.Student, needs to be put in a Group By statement. 最后,结果集中的任何非聚合函数结果的列(例如a.Student)都需要放入Group By语句中。

Edit: Version using our provided table/column names: 编辑:使用我们提供的表/列名称的版本:

Select b.StudentID, Count(b.QuizName) as TotalQuizCount, c.UniqueQuizCount
From [combine V2] as b
Inner Join (SELECT [a].StudentID, Count([a].QuizName) as UniqueQuizCount
from (SELECT Distinct StudentID, QuizName
FROM [combine V2] ) as a 
Group By a.StudentID) as c ON b.StudentID = c.StudentID
Group By b.StudentID, UniqueQuizCount

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM