Medium in Mysql database

Asked

Viewed 280 times

2

People I have the following scenario in the database:

Tables:

mdl_question_attempts (slot, questionid, rightanswer, responsesummary)

mdl_quiz_slots (slot, quizid, questionid)

On the table mdl_question_attempts attempts to respond to a particular quiz are recorded. questionid is the id of the issue, rightanswer is the correct answer, responsesummary is the answer given by the student.

On the table mdl_quiz_slots the questions that were applied to the students are recorded, where questionid is the id of the issue, quizid is the id of the questionnaire (set of questions).

I would like to have the average hit for a certain quiz.

  • What is the format of each field? rightanswer would be a boolean?

  • 1

    Average with mysql avg() command

  • @RORSCHACH is unclear if he wants to average some grade. It seems to me that he wants to know how many times the question has been answered correctly on the total of times answered.

  • @Leonardoperson for each correct answer can be considered as positive value 1, the formats rightanswer and responsesummary is text, to know if it is correct should be considered only the lines where rightanswer = responsesummary, and responsesumarry is different from NULL

  • @Leonardopessoa is just that, how many times the question has been answered correctly on the total of times the question has been answered. I think that I would have the media of every question, that?

  • The formula is in this question: https://answall.com/a/236471/64969 ; it is not in SQL, but gives the general idea of how to proceed. Just calculate the total of individuals / individuals of given group

  • @Jeffersonquesado I think the problem is a little more complex, I’m not a professional programmer so my difficulty. I can make the general media of the quiz (all questions) my difficulty is how to make the average by question, leading in cosnideration that a quiz can have different amounts of questions. this amount is obtained in the table mdl_quiz_slots where the quiz questions are registered

  • The modeling is the same: count individuals of the total population, count individuals who satisfy a certain condition, divide the second by the first.

  • my difficulty is in getting the result by question

  • Vdd dvd, you’re right, I’ll have to see how I can differentiate the questionnaires.

Show 5 more comments

1 answer

1


Taking into account that you just want to make the calculation described in the question (the average of hits per question), so I believe the table mdl_quiz_slots no need to enter the query. The query below returns:

  • id_question: id of the issue
  • total_answers: number of answers to each question
  • hits: number of hits
  • media_acertos: percentage of hits in relation to total answers to each question

Query:

select distinct(questionid) as id_questao,
count(questionid) as total_respostas,
sum(
   case when responsesummary = rightanswer
   then 1 else 0
   end
) as acertos,
concat(
   round(
      sum(
         case when responsesummary = rightanswer
         then 1 else 0
         end
      )
      / count(responsesummary) * 100
   ),'%'
) as media_acertos
from mdl_question_attempts
group by questionid
  • DVD, almost killed the problem! rsrs

  • DVD, almost killed the problem! rsrs is almost what I want, the table mdl_quiz_slots is who will sort the questions by questionnaires, for example, a questionnaire can have 20 questions, another 30 and so on, so I believe it would be necessary to do an INNER JOIN between mdl_quiz_slots and mdl_question_attempts, where can I put this INNER JOIN in your code? so I can do a Where to show only the average of a particular quiz (Quiz)

  • @Miguelsilva How could I give a inner join if the first table does not have a column related to the second table?

  • the slot column in the table mdl_questions_attempts gets value from the slot column of the table mdl_quiz_slots.

  • @Miguelsilva It’s because asked did not say. If you want some help.

  • sorry, didn’t really say, I’m facing problem when I do the Inner Join, of the two tables referencing questionid of the two tables, I get error saying that the expression questionid is ambiguous, I’m implementing in your code, know how to say what can be?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.