| Author |
Topic |
|
Byakuyaromania
Starting Member
2 Posts |
Posted - 2010-09-28 : 03:01:19
|
| Can someone help me with some queries?I have two tables answers and useranswers:Answers has the fields:id(auto-increment, primary key)questionid( id that points to a question)iscorrect( status of the answer: 1 for correct, 0 for incorrect)Useranswers has the fields:id(auto-increment, primary key)questionid( id that points to a question)answerid( id that points to the id from the table answers)sessionid( id for the session)What i want to do is find out how many wrong answers do i have.Each question can have multiple correct answers which need to be answered or a single correct answer.I need a query that can count the wrong answers and there are 3 cases for a wrong answer: -incomplete answer for a question when we have multiple correct answers; -when having multiple answers and the user selected too many answers of which one of them is incorrect, even if we have all correct answers; -when we have single answer for a question and it is incorrect.Please give me a query that works, i have tried alot of queries and none of them didn't work :( |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-09-28 : 03:17:17
|
| Can you please post some sample data & expected o/p?PBUH |
 |
|
|
Byakuyaromania
Starting Member
2 Posts |
Posted - 2010-09-28 : 03:43:13
|
| This is the answers table:id questionid iscorrect----------------------1, 1, 111, 1, 021, 1, 131, 31, 141, 31, 051, 21, 161, 21, 071, 41, 181, 41, 091, 41, 1101, 41, 0111, 51, 1121, 51, 0131, 51, 1This is for the useranswers table: id questionid answerid sessionid---------------------------------------------9301, 21, 61, 7119311, 31, 31, 7119321, 41, 71, 7119331, 51, 111, 7119341, 51, 131, 711The query result should be: 2 because there are 2 wrong answersthe results need to be shown for a session only not for all sessions |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-09-30 : 12:48:44
|
| [code]SELECT COUNT(*) AS WrongCntFROM(SELECT u.sessionid,u.questionid,COUNT(*) AS CntFROM answers aINNER JOIN useranswers uON u.questionid = a.questionidAND u.answerid = a.idAND a.iscorrect = 1GROUP BY u.sessionid,u.questionid)tINNER JOIN (SELECT questionid,COUNT(CASE WHEN iscorrect=1 THEN 1 ELSE NULL END) AS Cnt FROM answers GROUP BY questionid)t1ON t.questionid=t1.questionidWHERE t.Cnt <> t1.CntAND sessionid=<your sessionid value here>[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|