我从sql server获取季度记录。这些记录就像每年每个季度的总借记和贷记。在我的SQL查询中,我已经制作了季度,而且年份是动态的,如您在代码中所见。
public List<QuarterlyExpenseList> GetExpenseDataQuarterly(string Id, string Year)
{
string SQL = "select aspnetusers.username, SUM(case when Expense.Type='credit' and (Expense.Date>='@year-01-01' and Expense.Date<='@year-03-31') then Expense.Amount else 0 end) as QuarterOneCredits,";
SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>='@year-04-01' and Expense.Date<='@year-06-30') then Expense.Amount else 0 end) as QuarterTwoCredits,";
SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>='@year-07-01' and Expense.Date<='@year-09-30') then Expense.Amount else 0 end) as QuarterThreeCredits,";
SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>='@year-10-01' and Expense.Date<='@year-12-31') then Expense.Amount else 0 end) as QuarterFourCredits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>='@year-01-01' and Expense.Date<='@year-03-31') then Expense.Amount else 0 end) as QuarterOneDebits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>='@year-04-01' and Expense.Date<='@year-06-30') then Expense.Amount else 0 end) as QuarterTwoDebits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>='@year-07-01' and Expense.Date<='@year-09-30') then Expense.Amount else 0 end) as QuarterThreeDebits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>='@year-10-01' and Expense.Date<='@year-12-31') then Expense.Amount else 0 end) as QuarterFourDebits";
SQL += " from Expense inner join AspNetUsers on AspNetUsers.Id=Expense.MadeBy";
if (Id == null)
{
SQL += " group by aspnetusers.username";
}
else
{
SQL += " where Expense.MadeBy=@id group by AspNetUsers.UserName group by aspnetusers.username";
}
using (IDbConnection cn=Connection)
{
cn.Open();
List<QuarterlyExpenseList> objList = cn.Query<QuarterlyExpenseList>(SQL, new { year = Year, id = Id }).ToList();
return objList;
}
}
这里我得到Conversion failed when converting date and/or time from character string.
的例外。有人可以告诉我这是什么意思,我怎么能摆脱它
答案 0 :(得分:5)
我删除了充满活力的一年&#34; @ year&#34;外部字符串,因为它是动态值。如果写得像@年04-01&#39; SQL正在处理&#34; @ year&#34;作为字符串而不是动态值。
使用以下代码:
public List<QuarterlyExpenseList> GetExpenseDataQuarterly(string Id, string Year)
{
string SQL = "select aspnetusers.username, SUM(case when Expense.Type='credit' and (Expense.Date>=@year+'-01-01' and Expense.Date<=@year+'-03-31') then Expense.Amount else 0 end) as QuarterOneCredits,";
SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>=@year+'-04-01' and Expense.Date<=@year+'-06-30') then Expense.Amount else 0 end) as QuarterTwoCredits,";
SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>=@year+'-07-01' and Expense.Date<=@year+'-09-30') then Expense.Amount else 0 end) as QuarterThreeCredits,";
SQL += " SUM(case when Expense.Type='credit' and (Expense.Date>=@year+'-10-01' and Expense.Date<=@year+'-12-31') then Expense.Amount else 0 end) as QuarterFourCredits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>=@year+'-01-01' and Expense.Date<=@year+'-03-31') then Expense.Amount else 0 end) as QuarterOneDebits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>=@year+'-04-01' and Expense.Date<=@year+'-06-30') then Expense.Amount else 0 end) as QuarterTwoDebits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>=@year+'-07-01' and Expense.Date<=@year+'-09-30') then Expense.Amount else 0 end) as QuarterThreeDebits,";
SQL += " SUM(case when Expense.Type='debit' and (Expense.Date>=@year+'-10-01' and Expense.Date<=@year+'-12-31') then Expense.Amount else 0 end) as QuarterFourDebits";
SQL += " from Expense inner join AspNetUsers on AspNetUsers.Id=Expense.MadeBy";
if (Id == null)
{
SQL += " group by aspnetusers.username";
}
else
{
SQL += " where Expense.MadeBy=@id group by AspNetUsers.UserName group by aspnetusers.username";
}
using (IDbConnection cn=Connection)
{
cn.Open();
List<QuarterlyExpenseList> objList = cn.Query<QuarterlyExpenseList>(SQL, new { year = Year, id = Id }).ToList();
return objList;
}
}