Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 A Query To find the most matches....

Author  Topic 

vravo
Starting Member

13 Posts

Posted - 2004-08-20 : 23:15:26
I'm trying to write a query to grade tests.. when you click submit it puts your mutiple choice answers into a database... (did this part)

Joe |Question1| A
Joe |Question2| C

Then I want to run a query to check everyones answers against the answer sheet and tell me the scores

Joe got 45 out of 50
Steve got 41 out of 50

I'm having a tough time doing this as a query...

I can't do this in VB Checking the answers but more code and i think it would be faster as a query....

Thanks for any tips or ideas....

Vin


Thanks

Vin

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2004-08-21 : 00:13:21
Assumming you have a table with the following.

Test, Question, Answer
1 Question1 A
1 Question2 B

Name, Test, Question, Answer
Joe 1 Question1, A
Joe 1 Question2 B
Mike 1 Question1 C
Mike 1 Question2 D

It would go something like

SELECT AA.Name, AA.Test, AA.NumberOfCorrectAnswers/AA.NumberOfQuestions*100.00 As PercentageCorrect
FROM
(
Select
A.Name, A.Test, Count(A.*) As NumberOfCorrectAnswers,
(select count(*) from Answers where Test=1) as NumberOfQuestions from
Tests A inner join Answers B on A.Test=B.Test and A.Question=B.Question and A.Answer=B.Answer
where A.Test = 1
group by A.Name, A.Test
) AA
Go to Top of Page

vravo
Starting Member

13 Posts

Posted - 2004-08-21 : 00:44:45
I will try it.... Thanks...

Thanks

Vin
Go to Top of Page

vravo
Starting Member

13 Posts

Posted - 2004-08-21 : 12:16:39
Ok I get an error with "Count(A.*) As NumberOfCorrectAnswers" part it says Incorrect syntax near '*' on that line. No sure but is this doing a record count of every row with test "1" to determine the total number of questions...

Thanks Vin

Thanks

Vin
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-21 : 13:43:29
Try this one:

declare @testKey table (testId int, questionId int, answer varchar(1))
Insert Into @testKey
Select 1, 1, 'A'
Union All Select 1, 2, 'B'
Union All Select 1, 3, 'A'

declare @studentAnswers table (studentId int, testId int, questionId int, answer varchar(1))
Insert Into @studentAnswers
Select 1, 1, 1, 'A'
Union All Select 1, 1, 2, 'B'
Union All Select 1, 1, 3, 'C'
Union All Select 2, 1, 1, 'B'
Union All Select 2, 1, 2, 'C'
Union All Select 2, 1, 3, 'B'
Union All Select 3, 1, 1, 'A'
Union All Select 3, 1, 2, 'B'
Union All Select 3, 1, 3, 'A'


Select
A.StudentId,
A.testId,
QCnt = count(*),
Correct = sum(case when A.answer = B.answer then 1 else 0 end),
Grade = convert(money,sum(case when A.answer = B.answer then 1 else 0 end))/convert(money,count(*))*100
From @studentAnswers as A
Left Join @testKey as B
On A.testId = B.testId
and A.questionId = B.questionId
Group By A.StudentId, A.TestId


Corey
Go to Top of Page

vravo
Starting Member

13 Posts

Posted - 2004-08-23 : 12:30:04
Thanks all i appreciate the help that last on worked...


Vin

Thanks

Vin
Go to Top of Page
   

- Advertisement -