Campos no Select mais não no Group By

Ai pmlm após a inserção da sua query está aparecendo os seguinte erros Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '.'. Msg 102, Level 15, State 1, Line 80 Incorrect syntax near ')'. , mais antes eu queria que vc me explica-se as linhas de códigos

E

Para os registos com total > 3000 devolve o ano, porque primeiro queres ordernar por ano. Para os outros devolve 9999 como ano porque queres que apareçam depois.

Em relação aos erros não estou a ver motivo para eles agora, tenta fazer novamente sem o row_number para tentar despistar onde estará o erro.

Ai pmlm, eu conseguir reduzir a princípio os erros para 1 só com a seguinte query.:

    select row_number() OVER(t.ordem, t.TOTAL desc) FROM    
    (      select  min(year(ede.data)) as ORDEM,      
              eee.nome                        as 'NOME',       
              +'R$  ' +convert(varchar(10),sum(ede.valortot))      as 'TOTAL',      
                case      
                    when matcpl.codcur = 1 then 'DIREITO'      
                    when matcpl.codcur = 3 then 'MEDICINA'      
                    when matcpl.codcur = 5 then 'SISTEMAS DE INFORMAÇÃO'      
                    when matcpl.codcur = 6 then 'NUTRIÇÃO'      
                    when matcpl.codcur = 7 then 'ADMINISTRAÇÃO'      
                    when matcpl.codcur = 8 then 'EDUCAÇÃO FÍSICA'                             
                end as 'CURSO',       
              max(turma.turma)                  as 'TURMA',      
              isnull(eee.telaluno,'-')        as 'TELEFONE'      
        from eee,      
             matcpl,      
             fl,       
             turma,       
             ede      
        where      
             fl.valorbaixado   = 0.0000      
        and fl.datapag is null      
        and fl.databaixa is null        
        and ede.valorpago  = 0      
        and ede.valortot > 0.00      
        and ede.datapag is null        
        and matcpl.mataluno  = eee.matricula      
        and matcpl.mataluno  = eee.mataluno      
        and matcpl.perletivo = eee.perletivo      
        and matcpl.codcur    = eee.codcur      
        and turma.matricula     = eee.matricula       
        and ede.mataluno   = eee.matricula      
        and ede.codlanc    = fl.idlan      
        and fl.statuslan in (0)      
        group by eee.nome,      
                  eee.telaluno,      
                  matcpl.codcur      
        having sum(ede.valortot) >3000)    
   UNION
     
   ( select  9999 as ORDEM,      
              eee.nome                        as 'NOME',       
              +'R$  ' +convert(varchar(10),sum(ede.valortot))              as 'TOTAL',      
                case      
                    when matcpl.codcur = 1 then 'DIREITO'      
                    when matcpl.codcur = 3 then 'MEDICINA'      
                    when matcpl.codcur = 5 then 'SISTEMAS DE INFORMAÇÃO'      
                    when matcpl.codcur = 6 then 'NUTRIÇÃO'      
                    when matcpl.codcur = 7 then 'ADMINISTRAÇÃO'      
                    when matcpl.codcur = 8 then 'EDUCAÇÃO FÍSICA'                             
                end as 'CURSO',       
              max(turma.turma)                  as 'TURMA',      
              isnull(eee.telaluno,'-')        as 'TELEFONE'      
        from eee,      
             matcpl,      
             fl,       
             turma,       
             ede      
        where      
             fl.valorbaixado   = 0.0000      
        and fl.datapag is null      
        and fl.databaixa is null        
        and ede.valorpago  = 0      
        and ede.valortot > 0.00      
        and ede.datapag is null        
        and matcpl.mataluno  = eee.matricula      
        and matcpl.mataluno  = eee.mataluno      
        and matcpl.perletivo = eee.perletivo      
        and matcpl.codcur    = eee.codcur      
        and turma.matricula     = eee.matricula       
        and ede.mataluno   = eee.matricula      
        and ede.codlanc    = fl.idlan      
        and fl.statuslan in (0)      
        group by eee.nome,      
                  eee.telaluno,      
                  matcpl.codcur      
        having sum(ede.valortot) <=3000)  
order by t.ordem, t.TOTAL desc[/code]
o único erro que ta aparencendo agora é esse
[code]Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '.'.

Falta o alias para os inner selects

Põe os dois select entre parêntesis e o t no fim.

[quote=pmlm]Falta o alias para os inner selects

Põe os dois select entre parêntesis e o t no fim.

[/quote]
To tentando inserir os parenteses aqui nos select’s mais não ta dando, como ficaria?-

Assim dá erro?

select t.* FROM      
    (      select  min(year(ede.data)) as ORDEM,        
              eee.nome                        as 'NOME',         
              +'R$  ' +convert(varchar(10),sum(ede.valortot))      as 'TOTAL',        
                case        
                    when matcpl.codcur = 1 then 'DIREITO'        
                    when matcpl.codcur = 3 then 'MEDICINA'        
                    when matcpl.codcur = 5 then 'SISTEMAS DE INFORMAÇÃO'        
                    when matcpl.codcur = 6 then 'NUTRIÇÃO'        
                    when matcpl.codcur = 7 then 'ADMINISTRAÇÃO'        
                    when matcpl.codcur = 8 then 'EDUCAÇÃO FÍSICA'                               
                end as 'CURSO',         
              max(turma.turma)                  as 'TURMA',        
              isnull(eee.telaluno,'-')        as 'TELEFONE'        
        from eee,        
             matcpl,        
             fl,         
             turma,         
             ede        
        where        
             fl.valorbaixado   = 0.0000        
        and fl.datapag is null        
        and fl.databaixa is null          
        and ede.valorpago  = 0        
        and ede.valortot > 0.00        
        and ede.datapag is null          
        and matcpl.mataluno  = eee.matricula        
        and matcpl.mataluno  = eee.mataluno        
        and matcpl.perletivo = eee.perletivo        
        and matcpl.codcur    = eee.codcur        
        and turma.matricula     = eee.matricula         
        and ede.mataluno   = eee.matricula        
        and ede.codlanc    = fl.idlan        
        and fl.statuslan in (0)        
        group by eee.nome,        
                  eee.telaluno,        
                  matcpl.codcur        
        having sum(ede.valortot) >3000      
   UNION  
    select  9999 as ORDEM,        
              eee.nome                        as 'NOME',         
              +'R$  ' +convert(varchar(10),sum(ede.valortot))              as 'TOTAL',        
                case        
                    when matcpl.codcur = 1 then 'DIREITO'        
                    when matcpl.codcur = 3 then 'MEDICINA'        
                    when matcpl.codcur = 5 then 'SISTEMAS DE INFORMAÇÃO'        
                    when matcpl.codcur = 6 then 'NUTRIÇÃO'        
                    when matcpl.codcur = 7 then 'ADMINISTRAÇÃO'        
                    when matcpl.codcur = 8 then 'EDUCAÇÃO FÍSICA'                               
                end as 'CURSO',         
              max(turma.turma)                  as 'TURMA',        
              isnull(eee.telaluno,'-')        as 'TELEFONE'        
        from eee,        
             matcpl,        
             fl,         
             turma,         
             ede        
        where        
             fl.valorbaixado   = 0.0000        
        and fl.datapag is null        
        and fl.databaixa is null          
        and ede.valorpago  = 0        
        and ede.valortot > 0.00        
        and ede.datapag is null          
        and matcpl.mataluno  = eee.matricula        
        and matcpl.mataluno  = eee.mataluno        
        and matcpl.perletivo = eee.perletivo        
        and matcpl.codcur    = eee.codcur        
        and turma.matricula     = eee.matricula         
        and ede.mataluno   = eee.matricula        
        and ede.codlanc    = fl.idlan        
        and fl.statuslan in (0)        
        group by eee.nome,        
                  eee.telaluno,        
                  matcpl.codcur        
        having sum(ede.valortot) <=3000)  t   
order by t.ordem, t.TOTAL desc  

Ai pmlm como vai ser relatório pensei melhor, vou fazer 2 consultas diferentes, a primeira consulta vai mostrar os alunos que devem mais de 3000 reais ordenados por ede.data e sum(ede.valor) e a outra os alunos que devem menos de 3000 reais ordenaos por sum(ede.valor)… não preciso mostrar tudo numa query so… tem como vc me ajudar??

Assim é só separar os dois selects que estão no FROM…

[quote=pmlm]Assim é só separar os dois selects que estão no FROM…

[/quote]
Entendi vou tentar fazer aqui, só um detalhe, tem como substutuir aquele 9999 do select por outra coisa?

Se vais separar já não precisas sequer disso.

Ai pmlm após separar as querys executei primeiro a que vai ordenar por DATA(min(ede.data)) e por VALOR ( sum(ede.valortot)), porém me deparei com um resultado errado, como a primeira ordenação é pela data minima, tem que aparecer em primeiro plano o cara que tava com a data menor, e isso não está acontecendo, as datas estão aparecendo aleatóriamente… o resultado da query ta assim…


LINICKER está com a data 05/03/2009, e FLÁVIA LUCIA está com a data 09/02/2009, porem LINICKER está aparecendo primeiro do que ela, como a data dela é menor, ela tem que aparecer primeiro, se vc analisar melhor a imagem vai ver que isto está divergente, e está errado… to tentando corrigir aqui, tem alguma sugestão?
A Query é a seguinte

select ROW_NUMBER() over(order by min(ede.data),sum(ede.valortot)) as 'CLASSIFICAÇÃO', eee.nome as 'NOME', min(convert(char(10),ede.data,103)) as 'DATA', +'R$ ' +convert(varchar(10),sum(ede.valortot)) as 'TOTAL A PAGAR', case when matcpl.codcur = 1 then 'DIREITO' when matcpl.codcur = 3 then 'MEDICINA' when matcpl.codcur = 5 then 'SISTEMAS DE INFORMAÇÃO' when matcpl.codcur = 6 then 'NUTRIÇÃO' when matcpl.codcur = 7 then 'ADMINISTRAÇÃO' when matcpl.codcur = 8 then 'EDUCAÇÃO FÍSICA' end as 'CURSO', max(turma.turma) as 'TURMA', isnull(ee.telaluno,'-') as 'TELEFONE' from eee, matcpl, fl, turma, ede where fl.valorbaixado = 0.0000 and fl.datapag is null and fl.databaixa is null and ede.valorpago = 0 and ede.valortot > 0.00 and ede.datapag is null and matcpl.mataluno = eee.matricula and matcpl.mataluno = ede.mataluno and matcpl.perletivo = ede.perletivo and matcpl.codcur = ede.codcur and turma.matricula = eee.matricula and ede.mataluno = eee.matricula and ede.codlanc = fl.idlan and fl.statuslan in (0) group by eee.nome, eeee.telaluno, matcpl.codcur having sum(ede.valortot) > 3000 order by min(ede.data),sum(ede.valortot)

Muda

para

Tu queres a menor data e não a menor conversão da data (nota que são coisas diferentes)

[quote=pmlm]Muda

para

Tu queres a menor data e não a menor conversão da data (nota que são coisas diferentes)[/quote]

Ai pmlm, a princípio deu certo, vou modificar a segunda query, eu acho que vou ter problemas depois em juntar os dois selects no relatório pelo seguinte fato.: o Row_number do primeiro select vai trazer o rank dos alunos devedores com a dívida maior que 3000, o Row_number do segundo select também vai trazer o rank dos alunos devedores menor que 3000, porem, ao juntas esses dois select’s no relatório, após mostrar o ultimo registro dos alunos devedores do primeiro select, vai mostrar os alunos devedores do segundo select iniciando o rank como ‘1’, sendo que na verdade tinha que proseguir com a contagem… =/

Tenho que pensar numa forma para resolver isso!!

Como geras o relatório? Isso é um contador, deve haver maneira muito fácil de fazer isso sem ser no SQL.

Ai mano bom dia como vai?
Me falaram pra mim fazer essa consulta por UNION… como sería??

O UNION era como eu já tinha feito atrás, com dois selects unidos.

Entendi, vou fazer dessa maneira, só to vendo a questão do Row_number do segundo select, que vc colocou para aparecer 9999,

Ai mano, eu fiz a união das query de uma maneira diferente aqui que ficou como eu queria, só tem um porém, é quando inicia os dados do segundo select a classificação começa do 1º novamente, e o certo sería continuar, to tentando arrumar uma forma aqui…
o select ficou o seguinte.:

[code]select
ROW_NUMBER() over(order by min(ede.data),sum(ede.valortot) desc)
as ‘CLASSIFICAÇÃO’,
eee.nome as ‘NOME’,
convert(char(10),min(ede.data),103) as ‘DATA’,
+'R$ ’ +convert(varchar(10),sum(ede.valortot))
as ‘TOTAL A PAGAR’,
case
when matcpl.codcur = 1 then 'DIREITO’
when matcpl.codcur = 3 then 'MEDICINA’
when matcpl.codcur = 5 then 'SISTEMAS DE INFORMAÇÃO’
when matcpl.codcur = 6 then 'NUTRIÇÃO’
when matcpl.codcur = 7 then ‘ADMINISTRAÇÃO’
when matcpl.codcur = 8 then ‘EDUCAÇÃO FÍSICA’
end as ‘CURSO’,
max(turma.turma) as ‘TURMA’,
isnull(eee.telaluno,’-’) as 'TELEFONE’
from eee,
matcpl,
fl,
turma,
ede
where
fl.valorbaixado = 0.0000
and fl.datapag is null
and fl.databaixa is null
and ede.valorpago = 0
and ede.valortot > 0.00
and ede.datapag is null
and matcpl.mataluno = eee.matricula
and matcpl.mataluno = ede.mataluno
and matcpl.perletivo = ede.perletivo
and matcpl.codcur = ede.codcur
and turma.matricula = eee.matricula
and ede.mataluno = eee.matricula
and ede.codlanc = fl.idlan
and fl.statuslan in (0)
group by eee.nome,
eee.telaluno,
matcpl.codcur
having sum(ede.valortot) > 3000

UNION ALL

select
ROW_NUMBER() over(order by sum(ede.valortot)desc)
as ‘CLASSIFICAÇÃO’,
eee.nome as ‘NOME’,
convert(char(10),min(ede.data),103) as ‘DATA’,
+'R$ ’ +convert(varchar(10),sum(ede.valortot))
as ‘TOTAL A PAGAR’,
case
when matcpl.codcur = 1 then 'DIREITO’
when matcpl.codcur = 3 then 'MEDICINA’
when matcpl.codcur = 5 then 'SISTEMAS DE INFORMAÇÃO’
when matcpl.codcur = 6 then 'NUTRIÇÃO’
when matcpl.codcur = 7 then ‘ADMINISTRAÇÃO’
when matcpl.codcur = 8 then ‘EDUCAÇÃO FÍSICA’
end as ‘CURSO’,
max(turma.turma) as ‘TURMA’,
isnull(eee.telaluno,’-’) as 'TELEFONE’
from eee,
matcpl,
fl,
turma,
ede
where
fl.valorbaixado = 0.0000
and fl.datapag is null
and fl.databaixa is null
and ede.valorpago = 0
and ede.valortot > 0.00
and ede.datapag is null
and matcpl.mataluno = eee.matricula
and matcpl.mataluno = ede.mataluno
and matcpl.perletivo = ede.perletivo
and matcpl.codcur = ede.codcur
and turma.matricula = eee.matricula
and ede.mataluno = eee.matricula
and ede.codlanc = fl.idlan
and fl.statuslan in (0)
group by eee.nome,
eee.telaluno,
matcpl.codcur
having sum(ede.valortot) <= 3000

[/code]

Experimenta o primeiro select que está nesta página.

Pois é pmlm, esse select que postei ai é um aprimoramento do que vc postou, no teu select, o segundo select do union aparece 9999 para “Rank”, sendo que tinha que continuar a classficação do primeiro select…