C# DataTable-(乱七八糟-01)-查询日期区间内,统计不存在的维度值,显示且设置值为0

来源:互联网 发布:书画淘宝网 编辑:程序博客网 时间:2024/06/03 12:53
        //原始DataTable值        DataTable dt=.。。。。。。。。。。。。。        //维度类型        //逗比做法-把查询条件作为sql查询语句,包装到DataTable中        string companysql = "select " + com.GetQy(comp);        DataTable dtcomp = helper.GetDataTable(companysql);         //DataTable-行转列        DataTable dtt = new DataTable();        dtt.Columns.Add("category", typeof(string));        DataRow drt=dtt.NewRow();          for (int i = 0; i < dtcomp.Columns.Count; i++)         {            drt = dtt.NewRow();            drt["category"] = dtcomp.Rows[0][i];            dtt.Rows.Add(drt);        }        //日期        //逗比做法-把查询条件作为sql查询语句,包装到DataTable中        //当前做法-从其他DataTable表中取出不重复的日期值-DataView-实现        DataTable dtyear = null;        if (dt != null && dt.Rows.Count > 0)        {            DataView dv = new DataView(dt);            dtyear = dv.ToTable(true, "yearmonth");        }        //重新定义DataTable-封装转换后的的DataTable值        DataTable newdt = new DataTable();        newdt.Columns.Add("category", typeof(string));        newdt.Columns.Add("yearmonth", typeof(string));        newdt.Columns.Add("value", typeof(double));        DataRow drr = newdt.NewRow();        //匹配0值        if (dtyear.Rows.Count > 0)        {           //日期            for (int i = 0; i < dtyear.Rows.Count; i++)            {                //获取具体日期值                  string yearSD = dtyear.Rows[i]["yearmonth"].ToString();                //维度                for (int j = 0; j < dtt.Rows.Count; j++)                {                     drr = newdt.NewRow();                    //根据日期和维度-获取对应值                    DataRow[] drcomp = dt.Select(" category='" + dtt.Rows[j]["category"].ToString() + "' and yearmonth='" + yearSD+"' ");                     double yearTD = 0;//维度值                     //存在-对应值(唯一)                    if (drcomp.Length > 0)                    {                         drr["yearmonth"] = drcomp[0]["yearmonth"].ToString();//drcomp[0]["category"];                        drr["category"] =drcomp[0]["category"] ;                        drr["value"] = drcomp[0]["value"].ToString();                        newdt.Rows.Add(drr);                    }                    else//不存在-值设为0                    {                        drr["yearmonth"] = yearSD;                        drr["category"] = dtt.Rows[j]["category"].ToString();                        drr["value"] = yearTD;                        newdt.Rows.Add(drr);                    }                }             }            dt = newdt;        } 
阅读全文
0 0