现在做一功能,按月查询最后一条记录后与上月最后一条记录相应字段进行相减,算出每月用量。但存在一此特殊情况,比如1、本月没有匹配记录,我做的处理为设为本月相应字段用量为0;
2、本月有匹配记录,但上月无匹配记录,我做的处理为,取小于当前月的最后一条记录,进行运算,
问题是:
但上面每二条方法在本年内有记录的情况下是正确。但一直查上月无匹配记录,一直到1月一直都没有的话,此不会取上年的最后一条记录,所以计算数据是错。
如果解决。我的取上月的sql语句都是这样写的:
select top 1 * from waterDatasMonth where Name='{0}'and year(time)='{1}'and month(time)<'{2}'order by time desc",Name,dts,i); 其中dts为年时间,i为for循环,以取每个月的记录
谢谢!
2、本月有匹配记录,但上月无匹配记录,我做的处理为,取小于当前月的最后一条记录,进行运算,
问题是:
但上面每二条方法在本年内有记录的情况下是正确。但一直查上月无匹配记录,一直到1月一直都没有的话,此不会取上年的最后一条记录,所以计算数据是错。
如果解决。我的取上月的sql语句都是这样写的:
select top 1 * from waterDatasMonth where Name='{0}'and year(time)='{1}'and month(time)<'{2}'order by time desc",Name,dts,i); 其中dts为年时间,i为for循环,以取每个月的记录
谢谢!
假设dts为一个完整datetime,利用AddMonths对整个事件的月份进行递减for ( int i = 0; i >-100; i-- )
{
select top 1 * from waterDatasMonth where Name='{0}'and time='{1}' order by time desc",Name,dts.AddMonths(i);
其中dts为时间,i为for循环,利用AddMonths对年月不断的进行递减,以取每个月的记录然后对数据查出来的数据进行判断,如果查询有值,就break掉整个循环}思路是这个样子。
循环:
年份=y
月份=m -1循环
if月份循环到某年1月无匹配记录
年份=y-1
月份重置为m重新循环。
public DataTable GetUseAllDatas(string Name,string Year)
{
DataTable dt1 =new DataTable();
dt1.Columns.Add("WatNo",typeof(Int32));
dt1.Columns.Add("name",typeof(string));
dt1.Columns.Add("allBuyAmount",typeof(Int32));
dt1.Columns.Add("leftAmount",typeof(Int32));
dt1.Columns.Add("iniAmount",typeof(Int32));
dt1.Columns.Add("allAmount",typeof(Int32));
dt1.Columns.Add("amount",typeof(Int32));
dt1.Columns.Add("waterlevel",typeof(Int32));
dt1.Columns.Add("bengState",typeof(string));
dt1.Columns.Add("allowState",typeof(string));
dt1.Columns.Add("exp",typeof(string));
dt1.Columns.Add("time");
dt1.Columns.Add("warnState",typeof(string));
dt1.Columns.Add("allMony",typeof(decimal));
dt1.Columns.Add("leftMony",typeof(decimal));
string dts=Year;
for(int i=1;i<=12;i++)
{
str=string.Format("select top 1 * from waterDatasMonth where Name='{0}'and year(time)='{1}'and month(time)='{2}'order by time desc",Name,dts,i);
da=new SqlDataAdapter(str,con.GetConnection());
cmd=new SqlCommand(str,con.GetConnection());
dr1 =cmd.ExecuteReader();
DataSet ds=new DataSet();
da.Fill(ds,"useall");
while(dr1.Read())
{
DataRow drw = ds.Tables["useall"].Rows[0];
DataRow ddr = dt1.NewRow();
ddr.ItemArray = drw.ItemArray;
dt1.Rows.Add(ddr);
if(ds.Tables["useall"].Rows.Count!=0)
{
int x=i-1;
if(i==1)
{
str=string.Format("select top 1 * from waterDatasMonth where Name='{0}'and year(time)<'{1}' order by time desc",Name,dts);
}
else
{
str=string.Format("select top 1 * from waterDatasMonth where Name='{0}'and year(time)='{1}'and month(time)<'{2}'order by time desc",Name,dts,i); }
cmd=new SqlCommand(str,con.GetConnection());
dr1=cmd.ExecuteReader();
while(dr1.Read())
{
int a1=int.Parse(ds.Tables["useall"].Rows[0][5].ToString())-int.Parse(dr1.GetValue(5).ToString());
System.Windows.Forms.MessageBox.Show(dr1.GetValue(5).ToString());
dt1.Rows[x][5]=a1;
dt1.Rows[x][10]=i+"月份";
}
}
}
if(ds.Tables["useall"].Rows.Count==0)
{
if(i<=DateTime.Now.Month)
{
System.Windows.Forms.MessageBox.Show("aaa");
DataRow dds =dt1.NewRow();
dds[1]=Name;
dds[5]="0";
dds[11]="本月无用水记录";
dds[10]=i+"月份";
dt1.Rows.Add(dds);
}
else if(Year!=DateTime.Now.Year.ToString())
{
System.Windows.Forms.MessageBox.Show("aaa");
DataRow dds =dt1.NewRow();
dds[1]=Name;
dds[5]="0";
dds[11]="本月无用水记录";
dds[10]=i+"月份";
dt1.Rows.Add(dds);
}
} }
da.Dispose();
dr1.Close();
return dt1;
}