简体   繁体   中英

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). How can I do this in Access?

You want to use the DISTINCT predicate in your SQL query. 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.

An sql to get the count of unique quizzes a student took, would look like this:

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. 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.

The as a and as QuizCount is called aliasing. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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