I am developing an intranet web application that provides the users with short quizzes. The quizzes will be sent to the users via emails. I have the following database design:
Employee Table: Username, Name, Job, DivisionID
Division Table: DivisionID, DivisionName
Quiz Table: QuizID, Title, Description, IsSent
UserQuiz Table: UserQuizID, Score, DateTimeComplete, QuizID, Username
NOTE: The first attribute in each table is the primary key.
And for your information, IsSent is a flag used to determine which quiz is sent to the users and which one is not.
What I want and need now is: I need to come up with a query that shows the names of the non-participants in the last sending quiz. It means if there is more than one quiz that is sent to the users, this query should display the names of non-participants in the last sending quiz. The query also should display the DivisionName, too.
I came up with the following query but it shows the non-participants in all quizzes in the database. So how to modify it? or how to get what is required above?
SELECT TOP (100) PERCENT Q.Title, D.DivisionShortcut, E.Name
FROM dbo.Quiz AS Q CROSS JOIN
dbo.employee AS E LEFT OUTER JOIN
dbo.Divisions AS D ON E.DivisionCode = D.SapCode
WHERE (NOT EXISTS
(SELECT UserQuizID, QuizID, DateTimeComplete, Score, Username
FROM dbo.UserQuiz AS UQ
WHERE (Username = E.Username) AND (QuizID = Q.QuizID)))
ORDER BY Q.Title, D.DivisionShortcut
Last quiz sent is
users that participated at last quiz:
users that did not participate at the last quiz