Adjust query sql according to available discipline

Asked

Viewed 34 times

0

I have this query, working well, but it only works if I have data for the two disciplines PORTUGUES and MATEMATICA, how can I do so that, without harming the result, regardless of there being only one or two disciplines I can get the results, follows below only the part of the query that I think necessary, if I need to put the rest.

-- Campos a selecionar.
SELECT
AVG(IFNULL(pqas.fraction, 0)) * 10 AS MediaPT,
AVG(IFNULL(mqas.fraction, 0)) * 10 AS MediaMT,
gg.finalgrade AS NotaGeral,
u.firstname AS Aluno,
u.username AS Usuario,
u.lastname AS siem,
--  u.id, -- removido
u.department AS Turma,
u.institution AS Escola,
pqc.name AS catp,
mqc.name AS catm

FROM mdl_user u

INNER JOIN mdl_grade_grades gg ON gg.userid = u.id
INNER JOIN mdl_grade_items gi ON gi.`id` = gg.`itemid`
INNER JOIN mdl_question_attempt_steps pqas ON u.id = pqas.userid
INNER JOIN mdl_question_attempts pqa ON pqa.id = pqas.questionattemptid
INNER JOIN mdl_question pq ON pq.id = pqa.questionid
INNER JOIN mdl_quiz_slots pqs ON pqs.questionid = pqa.questionid AND pqs.slot = pqa.slot
INNER JOIN mdl_quiz pquiz ON pquiz.id = pqs.quizid
INNER JOIN mdl_question_categories pqc ON pqc.id = pq.category
INNER JOIN mdl_question_attempt_steps mqas ON u.id = mqas.userid
INNER JOIN mdl_question_attempts mqa ON mqa.id = mqas.questionattemptid
INNER JOIN mdl_question mq ON mq.id = mqa.questionid
INNER JOIN mdl_quiz_slots mqs ON mqs.questionid = mqa.questionid AND mqs.slot = mqa.slot
INNER JOIN mdl_quiz mquiz ON mquiz.id = mqs.quizid
INNER JOIN mdl_question_categories mqc ON mqc.id = mq.category    

-- Parte do WHERE para filtrar apenas PORTUGUES nas tabelas p.
WHERE SUBSTR(pqc.name, 8) = "PORTUGUES"

-- Parte do WHERE para filtrar apenas matemática nas tabelas m.
AND SUBSTR(mqc.name, 8) = "MATEMATICA"

-- Final da query.
GROUP BY turma, u.id
ORDER BY turma ASC, `Aluno` ASC
  • 1

    Miguel, add JOINS from pqc and mqc for the question to be correct...

  • @Rbz added

  • 1

    I ordered the code, and I fixed the WHERE (what I had also wrong in the answer)

2 answers

2

How to do

You can use the clause OR:

SELECT
    AVG(IFNULL(pqas.fraction, 0)) * 10 AS MediaPT,
    AVG(IFNULL(mqas.fraction, 0)) * 10 AS MediaMT,
    gg.finalgrade AS NotaGeral,
    u.firstname AS Aluno,
    u.username AS Usuario,
    u.lastname AS siem,
--  u.id, -- removido
    u.department AS Turma,
    u.institution AS Escola,
    pqc.name AS catp,
    mqc.name AS catm
FROM mdl_user u

WHERE SUBSTR(pqc.name, 8) = "PORTUGUES"
   OR SUBSTR(mqc.name, 8) = "MATEMATICA"

GROUP BY turma, u.id
ORDER BY turma ASC, `Aluno` ASC

Explaining

Row:

WHERE SUBSTR(pqc.name, 8) = "PORTUGUES"
       OR SUBSTR(mqc.name, 8) = "MATEMATICA"

The function OR will cause the query to bring the results of:

SUBSTR(pqc.name, 8) = "PORTUGUES" OR (OR) SUBSTR(mqc.name, 8) = "MATEMATICA"

  • I changed the answer because I was without where (didn’t make sense)

  • @Wow, I didn’t even notice!

1


amended response after comments

Using the via validation OR (since the control fields are of different tables), add also a validation in the field query; if it exists (Portuguese or math), calculate the average.
This validation is required to display the two averages (if any) or only one:

SELECT
    CASE WHEN (SUBSTR(pqc.name, 8) = 'PORTUGUES') THEN (AVG(IFNULL(pqas.fraction, 0)) * 10) ELSE NULL END AS MediaPT,
    CASE WHEN (SUBSTR(mqc.name, 8) = 'MATEMATICA') THEN (AVG(IFNULL(mqas.fraction, 0)) * 10) ELSE NULL END AS MediaMT,
    gg.finalgrade AS NotaGeral,
    u.firstname AS Aluno,
    u.username AS Usuario,
    u.lastname AS siem,
--  u.id, -- removido
    u.department AS Turma,
    u.institution AS Escola,
    pqc.name AS catp,
    mqc.name AS catm

FROM mdl_user u
-- join demais tabelas

WHERE SUBSTR(pqc.name, 8) = 'PORTUGUES'
   OR SUBSTR(mqc.name, 8) = 'MATEMATICA'

GROUP BY turma, u.id
ORDER BY turma ASC, `Aluno` ASC
  • 1

    Man, it doesn’t work 'cause they’re different: pqc and mqc. From what I understand, they are different tables that are in -- joins necessários ..., -- Parte do WHERE para filtrar apenas PORTUGUES nas tabelas p. and -- Parte do WHERE para filtrar apenas matemática nas tabelas m.

  • 1

    this time I didn’t notice =p I will delete my answer

  • normals ;) ....

  • Joe, I was thinking, these are tables that don’t exist in the query; I think the question is wrong.

  • @rLims this way I won’t have problems in the result of the Portuguese and math note when I have both disciplines?

  • @Inhares the tables exist yes, they are in necessary joins had already thought about the OR, but it does not serve

  • 1

    @Miguelsilva understood the point, see if the amendment meets.

  • @rLinhares I’m testing

  • @rLinhares worked perfectly, thank you very much

  • Now a problem has arisen, when there is more than one class, it does not filter even using condition in Where to bring me only certain class

  • 1

    @Miguelsilva the ideal is to keep the focus of the question; as the question problem has already been solved, it is advisable to leave the question answered (so that someone seeking the solution of the original problem, find) and create a new question with the other question.

  • OK, I marked as resolved again

Show 7 more comments

Browser other questions tagged

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