public static List<CustomerAppraisalInfo> ListCustomerAppraisal(int pageIndex, int pageSize, string fromDate, string toDate, string branchId, string departmentId, string referType)
{
DateTime dCurrentTime = DateTime.Now;
int rowCount, startRow, count = 0; CustomerSurveyDAL.SurveyTemplateInfo surveyTemplate = CustomerSurveyDAL.SurveyTemplate.GetSurveyTemplateByType(referType);
if (surveyTemplate.surveyTemplateId==0)
{
surveyTemplate = CustomerSurveyDAL.SurveyTemplate.GetSurveyTemplateByType("1");
} List<CustomerSurveyDAL.SurveyEntryListNameInfo> surveyEntrylist = CustomerSurveyDAL.SurveyEntry.ListSurveyEntryName("", "", "80", surveyTemplate.surveyTemplateId.ToString(), "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""); List<CustomerAppraisalInfo> results = new List<CustomerAppraisalInfo>();
List<CustomerAppraisalInfo> tempResults = new List<CustomerAppraisalInfo>(); if (string.IsNullOrEmpty(fromDate) == false)
fromDate = DateTime.Parse(fromDate).ToString("u").Substring(0, 10);
if (string.IsNullOrEmpty(toDate) == false)
toDate = DateTime.Parse(toDate).ToString("u").Substring(0, 10) + " 23:59:59"; StringBuilder strSQL = new StringBuilder();
pageIndex--;
if (pageIndex < 0)
pageIndex = 0;
if (pageSize <= 0)
pageSize = 10;
//因为可能2行合并1行
startRow = pageIndex * pageSize;
rowCount = startRow + pageSize;
strSQL.Append("SELECT TOP ").Append(rowCount.ToString()); strSQL.Append(" b.customerId,SUM(b.actualScore) / COUNT(*)");
foreach (var item in surveyEntrylist)
{
strSQL.Append(",sum(case when c.surveyEntryName = '" + item.surveyEntryName + "' then c.actualScore else 0 end) / count(distinct(sr.requestId))");
}//动态添加获取到的项
strSQL.Append(" FROM FQ_ServiceRequest sr WITH(NOLOCK) LEFT JOIN KH_Survey b WITH(NOLOCK) ON sr.requestId = b.sourceId left join KH_SurveyEntry c WITH(NOLOCK)on b.surveyId = c.objectId ");
strSQL.Append(" WHERE b.customerId > 0 and b.reserveId3 = 0"); if (String.IsNullOrEmpty(fromDate) != true)
strSQL.Append(" AND sr.creationTime >= @fromDate "); if (String.IsNullOrEmpty(toDate) != true)
strSQL.Append(" AND sr.creationTime <= @toDate "); if (String.IsNullOrEmpty(branchId) != true)
strSQL.Append(" AND sr.requestBranchId = @branchId "); if (String.IsNullOrEmpty(departmentId) != true)
strSQL.Append(" AND sr.supportDepartmentId = @departmentId "); if (String.IsNullOrEmpty(referType) != true)
strSQL.Append(" AND sr.referType = @referType "); strSQL.Append(" GROUP BY b.customerId ORDER BY b.customerId "); string cmdText = strSQL.ToString(); ParmInfo[] parms = new ParmInfo[] {
new ParmInfo("@branchId", DataType.Int, 0, String.IsNullOrEmpty(branchId)? 0 : int.Parse(branchId)),
new ParmInfo("@departmentId", DataType.Int, 0, String.IsNullOrEmpty(departmentId)? 0 : int.Parse(departmentId.Trim())),
new ParmInfo("@referType", DataType.VarChar, 20, String.IsNullOrEmpty(referType)? "" : referType.Trim()),
new ParmInfo("@fromDate", DataType.VarChar, 20, String.IsNullOrEmpty(fromDate)? "": fromDate.Trim()),
new ParmInfo("@toDate", DataType.VarChar, 20, String.IsNullOrEmpty(toDate)? "": toDate.Trim())
}; DbConnection conn = ConnManager.OpenConnection(dbl, connectionString); try
{
DbDataReader rdr = dbl.ExecuteReader(conn, cmdText, parms); while (rdr.Read())
{
if (count >= startRow)
{ CustomerAppraisalInfo r = new CustomerAppraisalInfo();
r.fromDate = fromDate;
r.toDate = toDate;
r.customerId = rdr.IsDBNull(0) ? 0 : rdr.GetInt32(0);
r.surveyAverageScore = rdr.IsDBNull(1) ? 0 : rdr.GetInt32(1);
if (surveyEntrylist.ToArray().Length >= 1)
{
r.obj1 = rdr.IsDBNull(2) ? 0 : rdr.GetInt32(2);
r.objName1 = surveyEntrylist[0].surveyEntryName;
}
if (surveyEntrylist.ToArray().Length >= 2)
{
r.obj2 = rdr.IsDBNull(3) ? 0 : rdr.GetInt32(3);
r.objName2 = surveyEntrylist[1].surveyEntryName;
}
if (surveyEntrylist.ToArray().Length >= 3)
{
r.obj3 = rdr.IsDBNull(4) ? 0 : rdr.GetInt32(4);
r.objName3 = surveyEntrylist[2].surveyEntryName;
}
if (surveyEntrylist.ToArray().Length >= 4)
{
r.obj4 = rdr.IsDBNull(5) ? 0 : rdr.GetInt32(5);
r.objName4 = surveyEntrylist[3].surveyEntryName;
}
if (surveyEntrylist.ToArray().Length >= 5)
{
r.obj5 = rdr.IsDBNull(6) ? 0 : rdr.GetInt32(6);
r.objName5 = surveyEntrylist[4].surveyEntryName;
}
if (surveyEntrylist.ToArray().Length >= 6)
{
r.obj6 = rdr.IsDBNull(7) ? 0 : rdr.GetInt32(7);
r.objName6 = surveyEntrylist[5].surveyEntryName;
}
if (surveyEntrylist.ToArray().Length >= 7)
{
r.obj7 = rdr.IsDBNull(8) ? 0 : rdr.GetInt32(8);
r.objName7 = surveyEntrylist[6].surveyEntryName;
}
if (surveyEntrylist.ToArray().Length >= 8)
{
r.obj8 = rdr.IsDBNull(9) ? 0 : rdr.GetInt32(9);
r.objName8 = surveyEntrylist[7].surveyEntryName;
}
if (surveyEntrylist.ToArray().Length >= 9)
{
r.obj9 = rdr.IsDBNull(10) ? 0 : rdr.GetInt32(10);
r.objName9 = surveyEntrylist[8].surveyEntryName;
}
if (surveyEntrylist.ToArray().Length >= 10)
{
r.obj10 = rdr.IsDBNull(11) ? 0 : rdr.GetInt32(11);
r.objName10 = surveyEntrylist[9].surveyEntryName;
} tempResults.Add(r);
}
count++;
} rdr.Close(); results = tempResults; foreach (CustomerAppraisalInfo r in tempResults)
{
if (r.customerId > 0)
r.customerName = Customers.GetCustomers(r.customerId).customerName;
}
ConnManager.CloseConnection(); return results;
}
catch (Exception e)
{
ConnManager.CloseConnection();
throw new ApplicationException(e.Message);
}
}

  

动态获取项加入到SQL中去统计的更多相关文章

  1. 关于sql中去换行符的问题

    今天要用bootstrap开发一个网页,要使用到JSON,但是JSON的格式不正确,然后在http://www.bejson.com/[Be JSON]中测试了一下JSON. 发现JSON中多了一个换 ...

  2. sql中数据统计

    今天来说一下使用sql统计数据. 用的H2数据库,用的是DBeaver连接工具.有三表,打印表PRINT_JOB,复印表COPY_JOB和扫描表SCANNER_JOB (这段可以忽略)任务是要统计相同 ...

  3. sql中如何统计一字段中字符串的个数

    declare @s varchar(100)set @s='156434A27kAsdABCiosd-01&**('--找出现的次数select len(@s)-len(replace(@s ...

  4. 数据库sql中distinct用法注意事项

    在写sql中去重复等操作,需要用到distinct. 在使用distinct的时候要注意,尤其是在有行列转换的时候.要把sql运行出来看看是不是与你想要的结果一样. 通过自己试验,distinct有从 ...

  5. PyQt(Python+Qt)学习随笔:QTreeWidgetItem项获取项的父项或子项

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 树型部件QTreeWidget中的QTreeWidgetItem项,可以通过child(int in ...

  6. Autofac 动态获取对象静态类获取对象

    Autofac 从容器中获取对象 静态类或Service场景可以动态,可以直接动态获取对象 /// <summary> /// 从容器中获取对象 /// </summary> ...

  7. KTHREAD 线程调度 SDT TEB SEH shellcode中DLL模块机制动态获取 《寒江独钓》内核学习笔记(5)

    目录 . 相关阅读材料 . <加密与解密3> . [经典文章翻译]A_Crash_Course_on_the_Depths_of_Win32_Structured_Exception_Ha ...

  8. Java中动态获取项目根目录的绝对路径

    https://www.cnblogs.com/zhouqing/archive/2012/11/10/2757774.html 序言 在开发过程中经常会用到读写文件,其中就必然涉及路径问题.使用固定 ...

  9. c# json转换成dynamic对象,然后在dynamic对象中动态获取指定字符串列表中的值

    using Newtonsoft.Json;using System;using System.Collections.Generic;using System.Linq;using System.T ...

随机推荐

  1. Visual Studio的Web Performance Test提取规则详解(2)

    总结 Visual Studio的Web Performance Test是基于HTTP协议层的,它不依赖于浏览器,通过直接接收,发送HTTP包来和Web服务器交互.Web Performance T ...

  2. (转)使用myeclipse生成实体类和hibernate映射文件

    转至:http://blog.sina.com.cn/s/blog_9658bdb40100uiod.html 1.下载并安装myeclipse,如果已经安装,则忽略该步骤; 2.打开myeclips ...

  3. [四校联考P3] 区间颜色众数 (主席树)

    主席树 Description 给定一个长度为 N 颜色序列A,有M个询问:每次询问一个区间里是否有一种颜色的数量超过了区间的一半,并指出是哪种颜色. Input 输入文件第一行有两个整数:N和C 输 ...

  4. 转:C++ Boost/tr1 Regex(正则表达式)快速指南

    C++ Boost/tr1 Regex(正则表达式)快速指南 正则表达式自Boost 1.18推出,目前已经成为C++11(tr1)的标准部分. 本文以Boost 1.39正则表达式为基础,应该广泛适 ...

  5. JQuery_简单选择器

    jQuery 最核心的组成部分就是:选择器引擎.它继承了 CSS 的语法,可以对 DOM 元素的标签名.属性名.状态等进行快速准确的选择,并且不必担心浏览器的兼容性. jQuery选择器实现了 CSS ...

  6. mybatis-config.xml简单笔记

    mybatis-config.xml简单笔记 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  7. contiki-main.c 文件的进程分析

    基本进程的创建实例: 共三部分:创建进程.进程自启动和进程的主体部分 /* 声明一个名为hello_world_process和led_process进程 PROCESS 宏实际上声明一个函数并定义一 ...

  8. 课时9—popup

    内容比较多的时候可以全屏显示,用第一种实现方式,如图1 图1 内容较少的话可以使用第二种实现方式如图2:图2 具体的实现代码如下: .header,.footer,.wrap-page{ positi ...

  9. Circular progress bar in Unity 3D

    Circular progress bar in Unity 3D - UnityScripthttp://stackoverflow.com/questions/22662706/circular- ...

  10. 51nod 1445 变色DNA(dij)

    题目链接:51nod 1445 变色DNA 看了相关讨论再去用最短路:val[i][j]之间如果是'Y',说明i可以到达j,并且i到达j的代价是i那行 1到j-1 里面'Y'的数量. 最后,求 0到n ...