动态获取项加入到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 ...
随机推荐
- Intellij IDEA debug介绍
先编译好要调试的程序. 1.设置断点 选定要设置断点的代码行,在行号的区域后面单击鼠标左键即可. 2.开启调试会话 点击红色箭头指向的小虫子,开始进入调试. IDE下方出现Debug视图,红色的箭头指 ...
- php进制转换函数
1 十进制(decimal system)转换函数 ① 十进制转二进制 string decbin(int number). 参数为一个十进制整型数字,不是整型数字会自动转为整型数字,如'3'转为3 ...
- [linux] mysql跨服务器访问, iptables设置
跨服务器mysql访问, iptables配置 1> 设置被访问机器的mysql权限 grant all privileges on *.* to root@"[ip]" i ...
- Java vs. C#
Java Program Structure C# package hello; public class HelloWorld { public static void main(String ...
- java高薪之路__004_泛型
参考地址: 1. http://www.cnblogs.com/lwbqqyumidi/p/3837629.html2. http://www.cnblogs.com/abcwt112/p/47350 ...
- 《高级Web应用程序设计》课程学习资料
任务1:什么是ASP.NET MVC 1.1 ASP.NET MVC简介 1.2 认识ASP.NET MVC项目结构 1.3 ASP.NET MVC生命周期 任务2:初识ASP.NET MVC项目开 ...
- 【转】CentOS 6.5安装pyspider过程记录
原文地址:http://blog.sina.com.cn/s/blog_48c95a190102wczx.html 1.根据pyspider官方推荐的安装方法,使用pip命令直接安装pyspider ...
- Python’s SQLAlchemy vs Other ORMs[转发 5] PonyORM
PonyORM PonyORM allows you to query the database using Python generators. These generators are trans ...
- mallo函数
malloc的全称是memory allocation,中文叫动态内存分配,当无法知道内存具体位置的时候,想要绑定真正的内存空间,就需要用到动态的分配内存.原型为extern void *malloc ...
- ios基础篇(十六)——UIWebView的基本使用
UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档等.UIWebView是一个混合体,具体的功能控件内置的,实现一些基本的功能.UIWebView可以查看Html网页,pdf文件,do ...