Fórum Agrupar colunas com 2 SQL Difrentes, é possível? #53589
26/10/2005
0
O que eu quero é o seguinte:
Preciso de um SQL para pegar o total da receita prevista por Mês e por Turma (é uma escola) e outro SQL para pegar o o quanto foi pago por mês e por Turma.
O problema é que não tem como fazer isso com SQL, pois são SQL Totalmente diferentes. Se não fosse o group by daria para fazer com sub-select.
Tem como eu Fazer 2 SQL juntar com outro. Tipo o Union mas o Union junta para baixo (linha). Eu quero juntas as colunas.
Ex.
COL1 COL2 || COLNoVA
Pode ser que exista outra solução, gostaria de sugestões.
obrigado.
Yallebr
Curtir tópico
+ 0Posts
26/10/2005
Emerson Nascimento
publique a instrução que você tentou e, se possível, publique também a estrutura das tabelas envolvidas.
Gostei + 0
27/10/2005
Gm.gui
Gostei + 0
27/10/2005
Yallebr
Estou tentando fazer com Sub - select ou Stored Procedure. Mas o problema está com o Group by.
Tabelas:
--Aluno--
IdAluno
[b:e04851cfc7]IdTurma Fk[/b:e04851cfc7]
--AlunosParcelas--
IdParcela
[b:e04851cfc7]IdAluno Fk[/b:e04851cfc7]
ValorBruto
ValorPago
Vencimento
O que eu preciso é pegar o Total a Receber por TURMA e por MES. E também o Total Recebido por Turma e por Mês.
---Total a Recebido-- select Sum(ValorPago) as TotalRecebido, extract(month from Vencimento) as Mes, A.IdTurma from AlunosParcelas AP left join ALUNOS A on A.IdAluno = AP.idaluno where Pago = 1 and extract(year from Vencimento) = 2005 group by 2,3 ---Total a Receber-- select Sum(ValorBruto) as TotalReceber, extract(month from Vencimento) as Mes, A.IdTurma from AlunosParcelas AP left join ALUNOS A on A.IdAluno = AP.idaluno where Pago = 0 and extract(year from Vencimento) = 2005 group by 2,3
O que eu quero é conseguir juntar esses 2 SQL em um só. Assim Ficaria.
IdTurma | Mes | TotalReceber | TotalRecebido
Fui claro? Caso não detalho melhor.
Obrigado.
Gostei + 0
27/10/2005
Emerson Nascimento
select A.idturma, extract(month from AP.Vencimento) as Mes, (select coalesce(sum(ap2.valorpago),0) from alunosparcelas ap2 inner join alunos a2 on (a2.idaluno=ap2.idaluno) where a2.idturma=a.idturma and ap2.pago=1 and extract(month from AP2.Vencimento)=extract(month from AP.Vencimento) ) ValorPago, (select coalesce(sum(ap2.valorbruto),0) from alunosparcelas ap2 inner join alunos a2 on (a2.idaluno=ap2.idaluno) where a2.idturma=a.idturma and ap2.pago=0 and extract(month from AP2.Vencimento)=extract(month from AP.Vencimento) ) ValorReceber from AlunosParcelas AP left join ALUNOS A on A.IdAluno = AP.idaluno where extract(year from AP.Vencimento) = 2005 group by 1, 2
note que algumas características/funções só estão disponíveis a partir do FB 1.5.XXXXX
Gostei + 0
28/10/2005
Yallebr
Gostei + 0
31/10/2005
Emerson Nascimento
select A.idturma, extract(month from AP.Vencimento) as Mes, sum(case when ap.pago=1 then ap.valorpago else 0 end) ValorPago, sum(case when ap.pago=0 then ap.valorbruto else 0 end) ValorReceber from AlunosParcelas AP left join ALUNOS A on A.IdAluno = AP.idaluno where extract(year from AP.Vencimento) = 2005 group by 1, 2
Gostei + 0
04/11/2005
Bruno Belchior
select A.idturma, extract(month from (case AP.Pagp = 0 then AP.Vencimento else AP.DataPagamento)) as Mes, sum(case when ap.pago=1 then ap.valorpago else 0 end) ValorPago, sum(case when ap.pago=0 then ap.valorbruto else 0 end) ValorReceber, sum(valorbruto) ValorTotalOriginal from AlunosParcelas AP left join ALUNOS A on A.IdAluno = AP.idaluno where extract(year from AP.Vencimento) = 2005 group by 1, 2
Gostei + 0
04/11/2005
Emerson Nascimento
select A.idturma, extract(month from (case when AP.Pago = 0 then AP.Vencimento else AP.DataPagamento end)) as Mes, sum(case when ap.pago=1 then ap.valorpago else 0 end) ValorPago, sum(case when ap.pago=0 then ap.valorbruto else 0 end) ValorReceber, sum(case when extract(month from (case when AP.Pago = 0 then AP.Vencimento else AP.DataPagamento end)) = extract(month from AP.Vencimento) then AP.ValorBruto else 0 end) ValorTotalOriginal from AlunosParcelas AP left join ALUNOS A on A.IdAluno = AP.idaluno where extract(year from AP.Vencimento) = 2005 group by 1, 2
Gostei + 0
07/11/2005
Bruno Belchior
Gostei + 0
08/11/2005
Emerson Nascimento
select A.idturma, extract(month from AP.Vencimento) as Mes, sum(case when ap.pago=1 then ap.valorpago else 0 end) ValorPago, sum(case when ap.pago=0 then ap.valorbruto else 0 end) ValorReceber, sum(AP.ValorBruto) ValorTotalOriginal from AlunosParcelas AP left join ALUNOS A on A.IdAluno = AP.idaluno where extract(year from AP.Vencimento) = 2005 group by 1, 2
Gostei + 0
08/11/2005
Bruno Belchior
SELECT A.IdTurma, EXTRACT(MONTH FROM (CASE WHEN pago = 0 THEN Vencimento ELSE DataPagamento END)) Mes, Coalesce(SUM(CASE WHEN pago = 1 THEN valorpago ELSE 0 END),0) TotalRecebido, Coalesce(SUM(CASE WHEN ap.pago = 0 THEN ap.valorbruto ELSE 0 END),0) TotalReceber, Coalesce((SELECT SUM(ap2.valorbruto) FROM alunosparcelas ap2 JOIN alunos a2 ON ap2.idaluno = a2.idaluno WHERE EXTRACT(MONTH FROM ap2.vencimento) = EXTRACT(MONTH FROM (CASE WHEN ap.pago = 0 THEN AP.Vencimento ELSE ap.DataPagamento END)) AND EXTRACT(YEAR FROM ap2.vencimento) = 2005 AND a2.idturma = a.idturma and AP2.IdProduto is null),0) ValorOriginal FROM AlunosParcelas AP JOIN ALUNOS A ON AP.IdAluno = A.IdAluno WHERE EXTRACT(YEAR FROM AP.Vencimento) = 2005 GROUP BY 1, 2
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)