Boa tarde Pessoal tenho a seguinte consulta no SQLServer 2005
[code]select
ROW_NUMBER() over(order by year(ede.data)) as ‘CLASSIFICAÇÃO’,
eee.nome as ‘NOME’,
ede.valortot as ‘TOTAL A PAGAR’,
uuu.nomecurso as ‘CURSO’,
mt.codturma as ‘TURMA’,
eee.telefone as ‘TELEFONE’,
from eee,
uuu,
mt,
mtcpl,
ede,
fl
where
mtcpl.status = 'MA’
and ede.valorpago = 0
and ede.valortot > 0.00
and ede.datapag is null
and fl.valorbaixado = 0.0000
and fl.datapag is null
and fl.databaixa is null
and mtcpl.mataluno = eee.matricula
and mtcpl.mataluno = mt.mataluno
and mtcpl.perletivo = mt.perletivo
and mtcpl.codcur = mt.codcur
and mtcpl.grade = mt.grade
and mtcpl.codcur = uuu.codcur
and mtcpl.mataluno = ede.mataluno
and mtcpl.perletivo = ede.perletivo
and mtcpl.codcur = ede.codcur
and mt.mataluno = ede.mataluno
and mt.perletivo = ede.perletivo
and mt.codcur = ede.codcur
and mt.mataluno = eee.matricula
and mt.codcur = uuu.codcur
and ede.mataluno = eee.matricula
and ede.codcur = uuu.codcur
and ede.codlanc = flan.idlan
group by eee.nome,
ede.valortot,
year(ede.data),
uuu.nome,
mt.codtur,
eee.telaluno[/code]
Ao executar a clausula ela retorna o seguinte resultado
[code]
CLASIFICAÇÃO | NOME TOTAL A PAGAR | CURSO | TURMA
1 JOÃO CALOS SOUZA 283.00 INFORMÁTICA ALFA
2 JOÃO CALOS SOUZA 283.00 INFORMÁTICA ALFA
3 JOÃO CALOS SOUZA 283.00 INFORMÁTICA ALFA
4 JOÃO CALOS SOUZA 283.00 INFORMÁTICA ALFA
5 MARIA PAULA ALVES OLIVEIRA 337.00 TESOURARIA GAMA_1
6 MARIA PAULA ALVES OLIVEIRA 337.00 TESOURARIA GAMA_2
7 MARIA PAULA ALVES OLIVEIRA 726.00 TESOURARIA GAMA_1
8 MARIA PAULA ALVES OLIVEIRA 726.00 TESOURARIA GAMA_2[/code]
agora eu tenho que calcular o total a pagar de JOÃO CALOS SOUZA e MARIA PAULA ALVES OLIVEIRA, utilizando somente a função SUM(ede.valortot) não ta dando.
utilizando a SUM(ede.valortot) o resultado aparece da seguinte maneira.:
[code]
CLASIFICAÇÃO | NOME TOTAL A PAGAR | CURSO | TURMA
1 JOÃO CALOS SOUZA 5660.00 NFORMÁTICA ALFA
5 MARIA PAULA ALVES OLIVEIRA 1011.00 TESOURARIA GAMA_1
6 MARIA PAULA ALVES OLIVEIRA 1011.00 TESOURARIA GAMA_2
7 MARIA PAULA ALVES OLIVEIRA 4356.00 TESOURARIA GAMA_1[/code]
Ainda da soma está errada depois de inserir o SUM(ede.valortot), MARIA PAULA ALVES OLIVEIRA está aparecendo mais de uma vez com valores distintos…
ja tentei todas as formas aqui e não conseguir…
ta diferente porque a maria esta em turmas distintas,
mas pode te mais alguma coisa, mas eu não vi.
na clausulas do seu select esta retornando o telefone tambem mas no resultado que vc postou eles não aparecem, porque?
[quote=DaniloAndrade]ta diferente porque a maria esta em turmas distintas,
mas pode te mais alguma coisa, mas eu não vi.
na clausulas do seu select esta retornando o telefone tambem mas no resultado que vc postou eles não aparecem, porque?[/quote]
ESqueci de colocar os telefones
JOÃO CALOS SOUZA = 9987-8854
MARIA PAULA ALVES OLIVEIRA = 8874-5532
roda novamente e coloca o resultado de novo com o telefone tambem
CLASIFICAÇÃO | NOME TOTAL A PAGAR | CURSO | TURMA
----------------------------------------------------------------------------- ------------------------------
1 JOÃO CALOS SOUZA 5660.00 NFORMÁTICA ALFA
5 MARIA PAULA ALVES OLIVEIRA 1011.00 TESOURARIA GAMA_1 // pode estar em anos diferentes
6 MARIA PAULA ALVES OLIVEIRA 1011.00 TESOURARIA GAMA_2
7 MARIA PAULA ALVES OLIVEIRA 4356.00 TESOURARIA GAMA_1 // pode estar em anos diferentes
//sera que não tem algum problema na função do ano
deixa ele agrupando por ano mas tira a classificação e mostra o ano no lugar e
CLASIFICAÇÃO | NOME TOTAL A PAGAR | CURSO | TURMA | TELEFONE
----------------------------------------------------------------------------- ------------------------------ -----------------------------
1 JOÃO CALOS SOUZA 283.00 INFORMÁTICA ALFA 9987-8854
2 JOÃO CALOS SOUZA 283.00 INFORMÁTICA ALFA 9987-8854
3 JOÃO CALOS SOUZA 283.00 INFORMÁTICA ALFA 9987-8854
4 JOÃO CALOS SOUZA 283.00 INFORMÁTICA ALFA 9987-8854
5 MARIA PAULA ALVES OLIVEIRA 337.00 TESOURARIA GAMA_1 8874-5532
6 MARIA PAULA ALVES OLIVEIRA 337.00 TESOURARIA GAMA_2 8874-5532
7 MARIA PAULA ALVES OLIVEIRA 726.00 TESOURARIA GAMA_1 8874-5532
8 MARIA PAULA ALVES OLIVEIRA 726.00 TESOURARIA GAMA_2 8874-5532
outra coisa quando vc coloca sum(ede.valortot) vc tem que tira o ede.valortot do group by, mas eu não tenho certeza, faz muito tempo que eu não faço query na mão rsrs, faz um teste e ve o que acontece
vc tirou o ede.valortot do group by? fiz um teste rapido aqui e o resultado com o campo que vc esta somando no group by da diferença
executa essa query e depois posta o resultado
select
year(ede.data) as 'Ano',
eee.nome as 'NOME',
ede.valortot as 'TOTAL A PAGAR',
uuu.nomecurso as 'CURSO',
mt.codturma as 'TURMA',
eee.telefone as 'TELEFONE',
from eee,
uuu,
mt,
mtcpl,
ede,
fl
where
mtcpl.status = 'MA'
and ede.valorpago = 0
and ede.valortot > 0.00
and ede.datapag is null
and fl.valorbaixado = 0.0000
and fl.datapag is null
and fl.databaixa is null
and mtcpl.mataluno = eee.matricula
and mtcpl.mataluno = mt.mataluno
and mtcpl.perletivo = mt.perletivo
and mtcpl.codcur = mt.codcur
and mtcpl.grade = mt.grade
and mtcpl.codcur = uuu.codcur
and mtcpl.mataluno = ede.mataluno
and mtcpl.perletivo = ede.perletivo
and mtcpl.codcur = ede.codcur
and mt.mataluno = ede.mataluno
and mt.perletivo = ede.perletivo
and mt.codcur = ede.codcur
and mt.mataluno = eee.matricula
and mt.codcur = uuu.codcur
and ede.mataluno = eee.matricula
and ede.codcur = uuu.codcur
and ede.codlanc = flan.idlan
group by eee.nome,
year(ede.data),
uuu.nome,
mt.codtur,
eee.telaluno
A diferença que ao invés de aparecer o rank aparece o ano…
Ai galera, eu conseguir chegar no resultado ‘Parcial’[u] que quero com essa consulta, porem quando retiro os comentários abaixo para aparecer a turma o resultado fica errado, e a turma da pessoa tem que aparecer, me da um help ai…
select
ROW_NUMBER() over(order by year(ede.data)) as 'CLASSIFICAÇÃO',
eee.nome as 'NOME',
sum(ede.valortot) as 'TOTAL A PAGAR',
case
when mtcpl.codcur = 1 then 'DIREITO'
when mtcpl.codcur = 3 then 'MEDICINA'
when mtcpl.codcur = 5 then 'SISTEMAS DE INFORMAÇÃO'
when mtcpl.codcur = 6 then 'NUTRIÇÃO'
when mtcpl.codcur = 7 then 'ADMINISTRAÇÃO'
when mtcpl.codcur = 8 then 'EDUCÃÇÃO FÍSICA'
end as 'CURSO',
//turma.codtur as 'TURMA', // <-- TENHO QUE COLOCAR ESSA LINHA
isnull(ealunos.telaluno,'-') as 'TELEFONE'
from eee,
mtcpl,
//turma, // <-- TENHO QUE COLOCAR ESSA LINHA
ede
where
mtcpl.status = 'MA'
and ede.valorpago = 0
and ede.valortot > 0.00
and ede.datapag is null
and mtcpl.mataluno = eee.matricula
and mtcpl.mataluno = ede.mataluno
and mtcpl.perletivo = ede.perletivo
and mtcpl.codcur = ede.codcur
// and mtcpl.perletivo = turma.codperlet
//and mtcpl.codcur = turma.codcur
group by eee.nome,
YEAR(ede.data),
eee.telaluno,
mtcpl.codcur
//turma.codtur // <-- TENHO QUE COLOCAR ESSA LINHA
Quando você usa o GROUP BY você está organizando os dados em GRUPOS para sumarizar alguma coisa. Se você colocar a coluna TURMA novamente, vai encontrar dois totalizadores para MARIA, um referente a GAMA_1 e outro referente a GAMA_2 e se vc tira-lo do GROUP BY ele vai totalizar, ignorando a turma dele.
Nao tem como vc colocar turma para aparecer na tupla, sem colocar TURMA no group by.
[quote=emanuelmm]Quando você usa o GROUP BY você está organizando os dados em GRUPOS para sumarizar alguma coisa. Se você colocar a coluna TURMA novamente, vai encontrar dois totalizadores para MARIA, um referente a GAMA_1 e outro referente a GAMA_2 e se vc tira-lo do GROUP BY ele vai totalizar, ignorando a turma dele.
Nao tem como vc colocar turma para aparecer na tupla, sem colocar TURMA no group by.
[/quote]
Pois é cara, eu tenho que colocar essa turma ai, não é por conta minha, foi solicitado para coloca-la!
Tenho que arrumar uma maneria de inserir essa turma no select
Entrao você vai ter que agrupar por turma… e ter os totalizadores da turma GAMA_1 e turma GAMA_2.
Mesmo assim vai continuar aparecendo as duas MARIA e os totais errados das tuplas!!
=/
Exatamente, pq vc tem duas turmas, se agrupar por turma vai mostrar o total da GAMA_1 e o total da GAMA_2. =]
Agrupando por turma, ainda do resultado do total a pagar aparecer todo errado, aparece mais de uma vez a mesma linha!!!
coloca turma no order by também