Select com join quem pode ajudar?

Delphi

23/01/2012

Pessoal,


Não estou conseguindo pegar as faltas do 3 bimestre (n3.faltas as faltas_3bim,) e 4 bimestre (n4.faltas as faltas_4bim), também não consigo pegar as notas do 3 bimestre(n3.nota as nota_3bim), 4 bimestre( n4.nota as nota_4bim), 1ª recuperação (n5.nota as nota_recuperacao_1), 2ª recuperação (n6.nota as nota_recuperacao_2) e 3ª recuperação (n7.nota as nota_recuperacao_3).

Agradeço antecipadamente quem poder ajudar.

Osmar


select n1.bimestre as bimestre_1,
n1.faltas as faltas_1bim,
n2.faltas as faltas_2bim,
n3.faltas as faltas_3bim,
n4.faltas as faltas_4bim,

n1.nota as nota_1bim,
n2.nota as nota_2bim,
n3.nota as nota_3bim,
n4.nota as nota_4bim,
n5.nota as nota_recuperacao_1,
n6.nota as nota_recuperacao_2,
n7.nota as nota_recuperacao_3,

n1.codigo_turma_notas as codigo_turma_1,

d1.nome_disciplina as nome_disciplina_1,

d1.carga_horaria_1bim as carga_horaria_1bim,
d1.carga_horaria_2bim as carga_horaria_2bim,
d1.carga_horaria_3bim as carga_horaria_3bim,
d1.carga_horaria_4bim as carga_horaria_4bim,
d1.aulas_dadas_1bim as aulas_dadas_1bim,
d1.aulas_dadas_2bim as aulas_dadas_2bim,
d1.aulas_dadas_3bim as aulas_dadas_3bim,
d1.aulas_dadas_4bim as aulas_dadas_4bim,


n1.escola

from notas n1 left join disciplina d1 on n1.disciplina_notas = d1.codigo
left join notas n2 on (n1.matricula_aluno_notas = n2.matricula_aluno_notas and
n1.bimestre <> n2.bimestre and
n2.bimestre <> 3 BIMESTRE and
n2.bimestre <> 4 BIMESTRE and
n2.bimestre <> 1 RECUPERACAO and
n2.bimestre <> 2 RECUPERACAO and
n2.bimestre <> 3 RECUPERACAO and
n2.bimestre <> 4 RECUPERACAO and

n1.codigo_turma_notas = n2.codigo_turma_notas and
n1.disciplina_notas = n2.disciplina_notas)

left join notas n3 on (n1.matricula_aluno_notas = n3.matricula_aluno_notas and
n1.bimestre <> n1.bimestre and
n2.bimestre <> n3.bimestre and
n3.bimestre <> 4 BIMESTRE and
n3.bimestre <> 1 RECUPERACAO and
n3.bimestre <> 2 RECUPERACAO and
n1.codigo_turma_notas = n3.codigo_turma_notas and
n1.disciplina_notas = n3.disciplina_notas)

left join notas n4 on (n1.matricula_aluno_notas = n4.matricula_aluno_notas and
n1.bimestre <> n4.bimestre and
n2.bimestre <> n4.bimestre and
n3.bimestre <> n4.bimestre and
n4.bimestre <> 1 RECUPERACAO and
n4.bimestre <> 2 RECUPERACAO and
n1.codigo_turma_notas = n4.codigo_turma_notas and
n1.disciplina_notas = n4.disciplina_notas)

left join notas n5 on (n1.matricula_aluno_notas = n5.matricula_aluno_notas and
n1.bimestre <> n5.bimestre and
n2.bimestre <> n5.bimestre and
n3.bimestre <> n5.bimestre and
n4.bimestre <> n5.bimestre and
n5.bimestre <> 2 RECUPERACAO and
n1.codigo_turma_notas = n5.codigo_turma_notas and
n1.disciplina_notas = n5.disciplina_notas)
left join notas n6 on (n1.matricula_aluno_notas = n6.matricula_aluno_notas and
n1.bimestre <> n6.bimestre and
n2.bimestre <> n6.bimestre and
n3.bimestre <> n6.bimestre and
n4.bimestre <> n6.bimestre and
n5.bimestre <> n6.bimestre and
n1.codigo_turma_notas = n6.codigo_turma_notas and
n1.disciplina_notas = n6.disciplina_notas)


left join notas n7 on (n1.matricula_aluno_notas = n7.matricula_aluno_notas and
n1.bimestre <> n7.bimestre and
n2.bimestre <> n7.bimestre and
n3.bimestre <> n7.bimestre and
n4.bimestre <> n7.bimestre and
n5.bimestre <> n7.bimestre and
n6.bimestre <> n7.bimestre and
n1.codigo_turma_notas = n7.codigo_turma_notas and
n1.disciplina_notas = n7.disciplina_notas)





where n1.matricula_aluno_notas =:aluno
and n1.codigo_turma_notas =:turma
and n1.bimestre = 1 BIMESTRE
order by n1.disciplina_notas
Osmar

Osmar

Curtidas 0

Respostas

Rod

Rod

23/01/2012


01-vamos por partes
02-esta confuso seu sql vamos quebrar ele e fazer aos poucos ex:
notei que possui tabela notas e Disciplina, esqueça por um momento do seu sql a tablea disciplinas.
03-se possivel passe o create table da tabelas notas.
04-se possivel passe 5 registros que vc tem inserido na tabela


select
bimestre, faltas, nota, codigo_turma_notas, escola
from
notas


Passe isto que fica mais facil de ajudarmos..
GOSTEI 0
Osmar

Osmar

23/01/2012

Rod,

Segue abaixo as duas tabelas, lembro que na tabela NOTAS inseri 7 registros que correspondem apenas a uma disciplina, se fossemos inserir outra disciplina, teriamos inseridos 14 registros.




TABELA NOTAS

(campo chave) (chave estrangeira)
CÓDIGO MATRICULA_ALUNO_NOTAS BIMESTRE FALTAS NOTA CÓDIGO_TURMA_NOTAS ANO DISCIPLINA_NOTAS ESCOLA
1 45 1 BIMESTRE 12 50 1 2012 1 1
2 45 2 BIMESTRE 20 60 1 2012 1 1
3 45 3 BIMESTRE 45 45 1 2012 1 1
4 45 4 BIMESTRE 26 46 1 2012 1 1
5 45 1 REUPERAÇÃO 0 51 1 2012 1 1
6 45 2 REUPERAÇÃO 0 47 1 2012 1 1
7 45 3 REUPERAÇÃO 0 90 1 2012 1 1


TABELA DISCIPLINA

CODIGO (campo chave)
NOME_DISCIPLINA
CÓDIGO_TURMA
CARGA_HORARIA_1BIM
CARGA_HORARIA_2BIM
CARGA_HORARIA_3BIM
CARGA_HORARIA_4BIM
AULAS_DADAS_1BIM
AULAS_DADAS_2BIM
AULAS_DADAS_3BIM
AULAS_DADAS_4BIM
ESCOLA (chave estrangeira)
GOSTEI 0
Osmar

Osmar

23/01/2012

Rod,

Segue abaixo as duas tabelas, lembro que na tabela NOTAS inseri 7 registros que correspondem apenas a uma disciplina, se fossemos inserir outra disciplina, teriamos inseridos 14 registros. O campo escola é o campo chave estrangeira.




TABELA NOTAS

(campo chave) (chave estrangeira)
CÓDIGO MATRICULA_ALUNO_NOTAS BIMESTRE FALTAS NOTA CÓDIGO_TURMA_NOTAS ANO DISCIPLINA_NOTAS ESCOLA
1 45 1 BIMESTRE 12 50 1 2012 1 1
2 45 2 BIMESTRE 20 60 1 2012 1 1
3 45 3 BIMESTRE 45 45 1 2012 1 1
4 45 4 BIMESTRE 26 46 1 2012 1 1
5 45 1 REUPERAÇÃO 0 51 1 2012 1 1
6 45 2 REUPERAÇÃO 0 47 1 2012 1 1
7 45 3 REUPERAÇÃO 0 90 1 2012 1 1


TABELA DISCIPLINA

CODIGO (campo chave)
NOME_DISCIPLINA
CÓDIGO_TURMA
CARGA_HORARIA_1BIM
CARGA_HORARIA_2BIM
CARGA_HORARIA_3BIM
CARGA_HORARIA_4BIM
AULAS_DADAS_1BIM
AULAS_DADAS_2BIM
AULAS_DADAS_3BIM
AULAS_DADAS_4BIM
ESCOLA (chave estrangeira)
GOSTEI 0
POSTAR