Count in multiple tables

0

I have 3 tables

Ineedaquerythattakesthenameoftheschool,amountofclassestypeA,qtdeofclassestypeB,qtdeofstudentswhoareinclassestypeA,andqtdeofstudentswhoareinclassestypeB.>

Sameasthisexample:

I tried to do

Select E.NOME,
SUM(CASE WHEN C.TIPO = 4 THEN 1 ELSE 0 end) as CLTELE5A8,
SUM(CASE WHEN C.TIPO = 5 THEN 1 ELSE 0 end) as CLTELEMED,

But I do not know how I will do the student count

    
asked by anonymous 14.08.2017 / 19:03

1 answer

1

The simplest way to implement is through subselect . If this gives you a performance problem, you can try using a temporary table .

Select E.NOME,
    SUM(CASE WHEN C.TIPO = 4 THEN 1 ELSE 0 end) as CLTELE5A8,
    SUM(CASE WHEN C.TIPO = 5 THEN 1 ELSE 0 end) as CLTELEMED,
    (SELECT COUNT(1) FROM ALUNOS AA JOIN CLASSES CC ON CC.COD_CLASSE = AA.CODCLASSE WHERE CC.TIPO = 'A') as 'QTD_ALUNOS_A',
    (SELECT COUNT(1) FROM ALUNOS AA JOIN CLASSES CC ON CC.COD_CLASSE = AA.CODCLASSE WHERE CC.TIPO = 'A') as 'QTD_ALUNOS_B'
    
14.08.2017 / 19:26