1
I have a table of provas
and a table of alunos
who have no direct relationship, I still have a table of inscrição aluno
and a table of escolas
, as follows in the modeling below:
I need to create a select to bring how many students from each school are enrolled in each test.
I resorted to a gambiarra with great reservations because the SELECT
worked perfectly:
SELECT (SELECT count(ia.idaluno) FROM inscricaoaluno ia INNER JOIN alunos a ON a.idaluno = ia.idaluno
WHERE a.idpessoa_juridica = e.idpessoa_juridica AND ia.idprova = p.idprova) qtd, e.nome, e.idpessoa_juridica , p.descprova, p.idprova
FROM escolas e
INNER JOIN prova p ON 1 = 1
GROUP BY e.nome, e.idpessoa_juridica , p.descprova, p.idprova
ORDER BY e.nome;
"I created a nonexistent relationship" of the school table with the proof table using a INNER JOIN ON 1 = 1
, with this I listed all the tests for each school, if I have the following data for example:
This "nonexistent" relationship will bring me the following result:
With that I create a subquery in the data that brings me the amount of each student in each test according to the school that he studies.
Now comes that doubt, is that a gambit? The result is satisfactory, it works, but many people do not look kindly on the use of 1 = 1
@Marcelo, only now I saw that it is for Oracle. You can confirm the syntax of the last query (comma instead of
CROSS JOIN
)?– gmsantos
Yes, both of you
selects
perform in the same way, did not know this relationship of typecross
, but from what I see it’s the same thing I put the two after thefrom
correct?– MarceloBoni
That’s right. I prefer the first way because it is clearer what is happening in the query. A comma is easy to go unnoticed, but a
CROSS JOIN
nay =)– gmsantos
Regarding performance, in tests that I did here in my environment, the three forms have practically the same performance
– MarceloBoni