Fórum Query para o mês anterior? #596272
13/08/2018
0
AdoQuery1.Close;
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add(''Select Sum(Valor) as Expr1'');
AdoQuery1.SQL.Add(''from entradas'');
AdoQuery1.SQL.Add(''where Month(Data) = Month(Date()) and year(Data) = Year(Date())'');
AdoQuery1.Open;Só que eu gostaria de inserir uma outra query para fazer o comparativo desse resultado com o resultado do mês anterior, automaticamente, no onshow do form.
Assim, ao exibir o form, anova query faria o mesmo que essa, porém somaria os registros do mês anterior.
Alguém pode me ajudar com esse código?
Edilson Santiago
Curtir tópico
+ 0Posts
13/08/2018
Alex Lekao
Você pode tentar utilizar um subselect ou um cross apply para retornar o mês e os comparativos.
Atenciosamente,
Alex - Lekao
Gostei + 0
13/08/2018
Felipe Morais
Se a Query deve somar os valores do mês atual e do anterior, é possível fazer na mesma Query, tente assim:
AdoQuery1.Close;
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add(''Select Sum(Valor) as Expr1'');
AdoQuery1.SQL.Add(''from entradas'');
AdoQuery1.SQL.Add(''where (Month(Data) = Month(Date()) and year(Data) = Year(Date()) and Month(Data) - 1 = Month(Date()) - 1 and year(Data) = Year(Date()))');
AdoQuery1.Open;
Veja se isto ajuda, abraços.
Gostei + 0
14/08/2018
Emerson Nascimento
Select sum(case when Month(Data) = Month(Date()) then Valor else 0 end) as VlrMesAtual, sum(case when Month(Data) <> Month(Date()) then Valor else 0 end) as VlrMesAnterior from entradas where (Month(Data) = Month(Date()) and year(Data) = Year(Date())) or (Month(Data) = Month(Date()-30) and year(Data) = Year(Date()-30))
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)