Usuário com melhor resposta
sum com filtro de data

Pergunta
-
bom dia, como posso fazer essa query do sql server no link to sql:
SELECT SUM(CONTAS_PAGAR.VALOR) AS SOMA FROM CONTAS_PAGAR WHERE DATEPART(MONTH,CONTAS_PAGAR.DATA_VENCIMENTO) = DATEPART(MONTH,GETDATE())
CodSys Automação Comercial Cabo Frio - RJ
Respostas
-
Olá Thiago,
Como seu campo aceita valores nulos, faça assim:
varvalordespesas1 = context.CONTAS_PAGARs.Where(emp => emp.DATA_VENCIMENTO.HasValue && emp.DATA_VENCIMENTO.Value.Year == Ano_atual && emp.DATA_VENCIMENTO.Value.Month == Mes_atual).Sum(emp => emp.VALOR);
[]s!
Fernando Henrique Inocêncio Borba Ferreira
while(alive){ this.WriteCode(); }
Blog: http://ferhenriquef.wordpress.com/
Twitter: @ferhenrique- Marcado como Resposta thiago pires pereira segunda-feira, 12 de dezembro de 2011 15:56
Todas as Respostas
-
Thiago,
Deve ficar assim:
var valor = contasPagar.Where(c => c.Vencimento.Month == 12) .Sum(c => c.Valor);
ps.: o nome de alguns atributos pode variar conforme sua implementação, mas a lógica é essa.
[]s!
Fernando Henrique Inocêncio Borba Ferreira
while(alive){ this.WriteCode(); }
Blog: http://ferhenriquef.wordpress.com/
Twitter: @ferhenrique -
-
Existem registros com o mês passado por parâmetro?
[]s!
Fernando Henrique Inocêncio Borba Ferreira
while(alive){ this.WriteCode(); }
Blog: http://ferhenriquef.wordpress.com/
Twitter: @ferhenrique -
-
para selecionar o mês atual eu fiz dessa forma:
using (var context = new finançasContext(ConnectionString))
{
ListaPagar = (from emp in context.CONTAS_PAGARs
where emp.DATA_VENCIMENTO >= new DateTime(Ano_atual, Mes_atual, 01) && emp.DATA_VENCIMENTO <= new DateTime(Ano_atual, Mes_atual, DateTime.DaysInMonth(Ano_atual, Mes_atual))select emp).ToList();
}
return ListaPagar;
porem não to conseguindo colocar o sum nesse select
CodSys Automação Comercial Cabo Frio - RJ -
Tenta assim:
using (var context = new finançasContext(ConnectionString)) { var valor = context.CONTAS_PAGARs.Where(emp => emp.DATA_VENCIMENTO >= new DateTime(Ano_atual, Mes_atual, 01) && emp.DATA_VENCIMENTO <= new DateTime(Ano_atual, Mes_atual, DateTime.DaysInMonth(Ano_atual, Mes_atual))).Sum(emp => emp.NOME_DO_CAMPO_VALOR); }
[]s!
Fernando Henrique Inocêncio Borba Ferreira
while(alive){ this.WriteCode(); }
Blog: http://ferhenriquef.wordpress.com/
Twitter: @ferhenrique -
-
Olá Thiago,
Nesse caso a Lamba não existe o select.
Tente remomer a clausula Where e veja quanto retorna.
Depois tente voltar a clausula Where.
Verifique também se estes registro realmente possuem valor.
[]s!
Fernando Henrique Inocêncio Borba Ferreira
while(alive){ this.WriteCode(); }
Blog: http://ferhenriquef.wordpress.com/
Twitter: @ferhenrique -
to fazendo assim:
public static decimal GetPDespesas()
{
int Mes_atual = DateTime.Now.Month;
int Ano_atual = DateTime.Now.Year;
decimal valordespesas;
using (var context = new finançasContext(ConnectionString))
{
var valordespesas1 = context.CONTAS_PAGARs.Where(emp => emp.DATA_VENCIMENTO >= new DateTime(Ano_atual, Mes_atual, 01) && emp.DATA_VENCIMENTO <= new DateTime(Ano_atual, Mes_atual, DateTime.DaysInMonth(Ano_atual, Mes_atual))).Sum(emp => emp.VALOR);
valordespesas = Convert.ToDecimal(valordespesas1);
}
return valordespesas;
}
porem retorna 0
CodSys Automação Comercial Cabo Frio - RJ -
Tenta simplificar sua consulta dessa forma:
public static decimal GetPDespesas() { int Mes_atual = DateTime.Now.Month; int Ano_atual = DateTime.Now.Year; decimal valordespesas; using (var context = new finançasContext(ConnectionString)) { varvalordespesas1 = context.CONTAS_PAGARs.Where(emp => emp.DATA_VENCIMENTO.Year == Ano_atual && emp.DATA_VENCIMENTO.Month == Mes_atual).Sum(emp => emp.VALOR); valordespesas = Convert.ToDecimal(valordespesas1); } return valordespesas; }
[]s!
Fernando Henrique Inocêncio Borba Ferreira
while(alive){ this.WriteCode(); }
Blog: http://ferhenriquef.wordpress.com/
Twitter: @ferhenrique -
-
Seu campo DATA_VENCIMENTO é do tipo DateTime?
[]s!
Fernando Henrique Inocêncio Borba Ferreira
while(alive){ this.WriteCode(); }
Blog: http://ferhenriquef.wordpress.com/
Twitter: @ferhenrique -
sim, olha o erro que dar:
Error 1 'System.Nullable<System.DateTime>' does not contain a definition for 'Year' and no extension method 'Year' accepting a first argument of type 'System.Nullable<System.DateTime>' could be found (are you missing a using directive or an assembly reference?) D:\FinançasWinPhone\FinançasWinPhone\FinançasWinPhone\DBHelper.cs 89 93 FinançasWinPhone
CodSys Automação Comercial Cabo Frio - RJ -
Olá Thiago,
Como seu campo aceita valores nulos, faça assim:
varvalordespesas1 = context.CONTAS_PAGARs.Where(emp => emp.DATA_VENCIMENTO.HasValue && emp.DATA_VENCIMENTO.Value.Year == Ano_atual && emp.DATA_VENCIMENTO.Value.Month == Mes_atual).Sum(emp => emp.VALOR);
[]s!
Fernando Henrique Inocêncio Borba Ferreira
while(alive){ this.WriteCode(); }
Blog: http://ferhenriquef.wordpress.com/
Twitter: @ferhenrique- Marcado como Resposta thiago pires pereira segunda-feira, 12 de dezembro de 2011 15:56
-
continua o valor zero.
fiz um select no bd e me retornou esse valor:
SELECT SUM(CONTAS_PAGAR.VALOR) AS SOMA FROM CONTAS_PAGAR WHERE DATEPART(MONTH,CONTAS_PAGAR.DATA_VENCIMENTO) = DATEPART(MONTH,GETDATE())
valor: 32
CodSys Automação Comercial Cabo Frio - RJ -
Se executarmos essa consulta sem o Sum, ele retorna todos os registros previstos?
Esse seu SELECT, se incluirmos o campo ano como filtro, retorna o valor 32 tb?
[]s!
Fernando Henrique Inocêncio Borba Ferreira
while(alive){ this.WriteCode(); }
Blog: http://ferhenriquef.wordpress.com/
Twitter: @ferhenrique -
-