动态获取项加入到SQL中去统计
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中去统计的更多相关文章
- 关于sql中去换行符的问题
今天要用bootstrap开发一个网页,要使用到JSON,但是JSON的格式不正确,然后在http://www.bejson.com/[Be JSON]中测试了一下JSON. 发现JSON中多了一个换 ...
- sql中数据统计
今天来说一下使用sql统计数据. 用的H2数据库,用的是DBeaver连接工具.有三表,打印表PRINT_JOB,复印表COPY_JOB和扫描表SCANNER_JOB (这段可以忽略)任务是要统计相同 ...
- sql中如何统计一字段中字符串的个数
declare @s varchar(100)set @s='156434A27kAsdABCiosd-01&**('--找出现的次数select len(@s)-len(replace(@s ...
- 数据库sql中distinct用法注意事项
在写sql中去重复等操作,需要用到distinct. 在使用distinct的时候要注意,尤其是在有行列转换的时候.要把sql运行出来看看是不是与你想要的结果一样. 通过自己试验,distinct有从 ...
- PyQt(Python+Qt)学习随笔:QTreeWidgetItem项获取项的父项或子项
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 树型部件QTreeWidget中的QTreeWidgetItem项,可以通过child(int in ...
- Autofac 动态获取对象静态类获取对象
Autofac 从容器中获取对象 静态类或Service场景可以动态,可以直接动态获取对象 /// <summary> /// 从容器中获取对象 /// </summary> ...
- KTHREAD 线程调度 SDT TEB SEH shellcode中DLL模块机制动态获取 《寒江独钓》内核学习笔记(5)
目录 . 相关阅读材料 . <加密与解密3> . [经典文章翻译]A_Crash_Course_on_the_Depths_of_Win32_Structured_Exception_Ha ...
- Java中动态获取项目根目录的绝对路径
https://www.cnblogs.com/zhouqing/archive/2012/11/10/2757774.html 序言 在开发过程中经常会用到读写文件,其中就必然涉及路径问题.使用固定 ...
- c# json转换成dynamic对象,然后在dynamic对象中动态获取指定字符串列表中的值
using Newtonsoft.Json;using System;using System.Collections.Generic;using System.Linq;using System.T ...
随机推荐
- nginx简单的rewrite配置
假设当前已存在location /test/,希望配置一个短连接/ts/与之相同,就需要用到rewrite 直接配置到server段 rewrite ^/ts/(.*) /test/$1; 或者放到l ...
- ubuntu下安装、启动和卸载SSH
想往VMWare虚拟机上的Ubuntu里面拷贝代码,发现之前安装好的secureCRT链接不上.发现是ssh安装配置出了问题,于是就把openssh-server卸载后重装,发现又是与openssh- ...
- JS部分代码分享
2015-10-12 : 1.search()表达式:搜索索引 <!DOCTYPE html><html><body> <p>搜索字符串 "w ...
- 关于jQuery源码分析
http://www.w3ctech.com/topic/256 jQuery源码剖析(一)——概览&工具方法
- Android TextView自动换行文字排版参差不齐的原因
今天项目没什么进展,公司后台出问题了.看了下刚刚学习Android时的笔记,发现TextView会自动换行,而且排版文字参差不齐.查了下资料,总结原因如下: 1.半角字符与全角字符混乱所致:这种情况一 ...
- 关于实现自定义Dialog和实现Dialog里view的事件监听的两种方法
一.自定义dialog. 二.实现dialog里view的事件监听 1.自定义dialog比较简单.在实例化new的时候,加入样式,布局就行了.或者重写dialog. 2.实现dialog里view的 ...
- Linux变量
变量:(大的分为环境变量与本的变量) 本地变量: 本地变量在用户现在的shell生命期的脚本中使用.例如,本地变量file-name="loop.doc",这个值只在用户当前she ...
- 访问google.com
ping www.google.com 得到的IP来访问
- iOS - Apache Tomcat WebServer 服务器配置
前言 提前下载好相关软件,且安装目录最好安装在全英文路径下.如果路径有中文名,那么可能会出现一些莫名其妙的问题. 提前准备好的软件: apache-tomcat-6.0.45.tar.gz eclip ...
- Android 之计算控件颜色透明度
Android 之计算控件颜色透明度 1.UI会给一个数值,例如:#EFE000,透明度30% 2.用255乘以30%等于76.5,然后四舍五入等于77 3.用计算器将十进制的77转成十六进制的数据为 ...