1. 程式人生 > >一段經典的Group by 分頁程式

一段經典的Group by 分頁程式

        private DataTable GetDataTable(ModalAshxParams mp, out int TotalCount, bool pager = true)
        {
            var QueryParams = new
            {
                RiQiStart = mp.Request("RiQiStart"),
                RiQiEnd = mp.Request("RiQiEnd"),
                ZhouQi = mp.Request("ZhouQi")
            };
            System.Text.StringBuilder filter = new System.Text.StringBuilder();
            if (QueryParams.RiQiStart != "")
            {
                if (filter.ToString() != "")
                {
                    filter.Append(" and ");
                }
                filter.Append("統計日期 >='" + QueryParams.RiQiStart + "'");
            }
            if (QueryParams.RiQiEnd != "")
            {
                if (filter.ToString() != "")
                {
                    filter.Append(" and ");
                }
                filter.Append("統計日期 <='" + QueryParams.RiQiEnd + "'");
            }
            string fields = "'' as 統計週期,統計日期,avg(總電話量) as 總電話量,avg(已註冊聲紋電話量) as 已註冊聲紋電話量,avg(總錄音數量) as 總錄音數量,avg(客戶滿意數量) as 客戶滿意數量,avg(客戶不滿錄音數量) as 客戶不滿錄音數量,avg(坐席態度模型判斷非可疑錄音數) as 坐席態度模型判斷非可疑錄音數,avg(坐席態度模型判斷可疑錄音數) as 坐席態度模型判斷可疑錄音數,avg(客戶情緒指數) as 客戶情緒指數,avg(坐席態度指數) as 坐席態度指數";
            string groupby = "統計日期";
            string orderby = "統計日期 desc";
            if (QueryParams.ZhouQi == "日")
            {
                groupby = "統計日期";
            }
            else if (QueryParams.ZhouQi == "周")
            {
                fields = fields.Replace("統計日期", "datepart(ww,統計日期) as 統計日期");
                fields += ",datepart(yy,統計日期) as 統計年份";
                groupby = "datepart(ww,統計日期),datepart(yy,統計日期)";
                orderby = "datepart(yy,統計日期) desc,datepart(ww,統計日期) desc";
            }
            else
            {
                fields = fields.Replace("統計日期", "datepart(mm,統計日期) as 統計日期");
                fields += ",datepart(yy,統計日期) as 統計年份";
                groupby = "datepart(mm,統計日期),datepart(yy,統計日期)";
                orderby = "datepart(yy,統計日期) desc,datepart(mm,統計日期) desc";
            }

            //BLL.instance.WriteLog_Info("satify", filter.ToString());
            DataTable dt;

            //dt = new Entity.tblStatisticsDateIndex().Select_GroupBy_OrderBy(fields, filter.ToString(), groupby, mp.OrderField + " " + mp.Sorting).Tables[0];
            //TotalCount = dt.Rows.Count;
            string ssql = SqlClass.Util.GetSqlPage(Entity.tblStatisticsDateIndex.TN_TableName, fields, filter.ToString(), groupby, "", orderby, mp.Pageindex, mp.Pagesize);
            BLL.instance.WriteLog_Info("GetSqlPage", ssql);
            dt = new Entity.tblStatisticsDateIndex().SelectPageOutTotal(Entity.tblStatisticsDateIndex.TN_TableName, fields, filter.ToString(), groupby, "", orderby, mp.Pageindex, mp.Pagesize, out TotalCount, "", null).Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                if (QueryParams.ZhouQi == "日")
                {
                    dr["統計週期"] = Convert.ToDateTime(dr["統計日期"]).ToString("yyyy-MM-dd");
                }
                else if (QueryParams.ZhouQi == "周")
                {
                    dr["統計週期"] = dr["統計年份"].ToString() + "年" + "第" + dr["統計日期"].ToString() + "周";
                }
                else
                {
                    dr["統計週期"] = dr["統計年份"].ToString() + "年" + dr["統計日期"].ToString() + "月";
                }
            }
            return dt;
        }