mybatis报表,动态列与查询参数+行列转换

这是报表原型,在这张报表中,使用了动态的列与动态查询参数,动态列与动态查询参数全部使用map将参数传入
map参数:
//拼接查询时间
for (String month : monthList) {
List<LocalDate> dateList = new ArrayList<>();
String year1 = yearList.get(1);
String day1 = dayList.get(0);
String day2 = dayList.get(1);
LocalDate selectDateBegin = this.parseLocalDate(year1, month, day1);
LocalDate selectDateEnd = this.parseLocalDate(year1, month, day2);
dateList.add(selectDateBegin);
dateList.add(selectDateEnd);
dateMap.put(month, dateList);
String orderNumberSelect = "orderNumber" + month;
String averageOrderAmountSelect = "averageOrderAmount" + month;
String orderAmountSelect = "orderAmount" + month;
List<String> stringList = new ArrayList<>();
stringList.add(orderNumberSelect);
stringList.add(averageOrderAmountSelect);
stringList.add(orderAmountSelect);
columnMap.put(month, year1 + "-" + month);
}
//去年最后一月
List<LocalDate> dateListLastYear = new ArrayList<>();
LocalDate selectDateBegin = this.parseLocalDate(yearList.get(0), monthList.get(monthList.size() - 1), dayList.get(0));
LocalDate selectDateEnd = this.parseLocalDate(yearList.get(0), monthList.get(monthList.size() - 1), dayList.get(1));
dateListLastYear.add(selectDateBegin);
dateListLastYear.add(selectDateEnd);
dateMap.put(Constants.LAST_YEAR_SAME_MONTH, dateListLastYear);
columnMap.put(Constants.LAST_YEAR_SAME_MONTH, yearList.get(0) + "-" + monthList.get(monthList.size() - 1));
拼接出两个map,columnMap("09","2018 + 09"),dateMap("09",List("2018-09-01","2018-09-31"))
本来的查询:

使用这两个map作为动态参数传入,在mybatis中进行遍历,并且进行mysql的行列装换:
<select id="getCompany" parameterType="com.jn.ssr.superrescuereporting.web.entity.dto.CustomerMonthSearchDTO"
resultType="com.jn.ssr.superrescuereporting.web.entity.CustomerMonthEntity" statementType="STATEMENT">
select
<if test="param.findStatus == 1">
companyId,companyName,
</if>
<if test="param.findStatus == 0">
parentCompanyId as companyId,parentCompanyName as companyName,
</if>
<foreach collection="param.columnMap" index="month" item="item" separator=" ">
Max(case countDate when '${item}' then orderNumber else 0 end ) orderNumber${month},
Max(case countDate when '${item}' then orderAmount else 0 end ) orderAmount${month},
Max(case countDate when '${item}' then averageOrderAmount else 0 end )averageOrderAmount${month},
</foreach>
<if test="param.findStatus == 1">
parentCompanyId,parentCompanyName,
</if>serviceType from (
select serviceType,companyId,parentCompanyId,parentCompanyName,companyName,orderNumber,orderAmount,averageOrderAmount,countDate
from (select '汇总' serviceType,company.id companyId,company.parent_company_id parentCompanyId,
parentCompany.company_name parentCompanyName,company.company_name companyName,count(1) orderNumber,
sum(after_discount_amount) orderAmount,TRUNCATE(sum(after_discount_amount) / count(1), 2) averageOrderAmount,
date_format(t.create_time, '%Y-%m') countDate
from or_task_count t
join operate_service_type type on type.type = t.service_type
join sp_company company on company.id = t.company_id
join sp_company parentCompany on company.parent_company_id = parentCompany.id
<where>
and t.state = 2 and(
<foreach collection="param.dateMap" index="key" item="dateList" separator="or">
t.create_time between
<foreach collection="dateList" item="dateItem" separator=" and " open=" " close=" ">
'${dateItem}'
</foreach>
</foreach>)
</where>
group by company.id, date_format(t.create_time, '%Y-%m'), parentCompanyId, companyName
ORDER by parentCompanyId, company.id, date_format(t.create_time, '%Y-%m'))t)t
<if test="param.findStatus == 0">
group by parentCompanyId
</if>
<if test="param.findStatus == 1">
group by companyId
</if>
</select>
查询出来的效果为(行列装换后):

其中13代表去年同期
mybatis报表,动态列与查询参数+行列转换的更多相关文章
- MyBatis 实践 -动态SQL/关联查询
MyBatis 实践 标签: Java与存储 动态SQL 动态SQL提供了对SQL语句的灵活操作,通过表达式进行判断,对SQL进行拼接/组装. if 对查询条件进行判断,如果输入参数不为空才进行查询条 ...
- MyBatis的动态SQL操作--查询
查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL,即根据不同的情况生成不同的sql语句. 模拟一个场景,在做多条件搜索的时候,
- SQL行列转换6种方法
在进行报表开发时,很多时候会遇到行列转换操作,很对开发人员针对于SQL级别行列转换操作一直不甚理解,今天正好抽空对其进行了一些简单的总结.这里主要列举3种可以实现SQL行列转换的方法,包括通用SQL解 ...
- KingbaseES 行列转换函数
关键字: 行专列,列转行, pivot, unpivot 行列转换是在数据分析中经常用到的一项功能,KingbaseES从V8R6C3B0071版本开始通过扩展插件(kdb_utils_func ...
- Dynamic CRM 2013学习笔记(二十六)报表设计:Reporting Service报表 动态参数、参数多选全选、动态列、动态显示行字体颜色
上次介绍过CRM里开始报表的一些注意事项:Dynamic CRM 2013学习笔记(十五)报表入门.开发工具及注意事项,本文继续介绍报表里的一些动态效果:动态显示参数,参数是从数据库里查询出来的:参数 ...
- 8.mybatis动态SQL模糊查询 (多参数查询,使用parameterType)
多参数查询,使用parameterType.实例: 用户User[id, name, age] 1.mysql建表并插入数据 2.Java实体类 public class User { public ...
- 利用MyBatis的动态SQL特性抽象统一SQL查询接口
1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...
- mybatis的mapper特殊字符转移以及动态SQL条件查询
前言 我们知道在项目开发中之前使用数据库查询,都是基于jdbc,进行连接查询,然后是高级一点jdbcTemplate进行查询,但是我们发现还是不是很方便,有大量重复sql语句,与代码偶合,效率低下,于 ...
- [数据库] SQL查询语句表行列转换及一行数据转换成两列
原文来自:http://blog.csdn.net/Eastmount/article/details/50559008 本文主要讲述了SQL查询语句表之间的行列转换,同时也包括如何将一行数据转换成两 ...
随机推荐
- jvm 的性能调优
一个描述的比较生动的博客:JVM内存区域划分Eden Space.Survivor Space.Tenured Gen,Perm Gen解释 地址:https://www.cnblogs.com/sg ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException 异常
MySQL完整性约束破坏异常:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException 在单向多对一关联关系 ...
- OpenCV人脸识别
import cv2 filename = 'pic.jpg' def detect(filename): face_cascade = cv2.CascadeClassifier('./haarca ...
- Lucas 大组合数
题目:HDU 3037 题意:有n个树,m个坚果,放到n个树里,可以不放完,有多少种方法. 分析: 得到组合数了. 大组合数什么费马小定理,Lucas定理都来了: 总的说,不能用二维地推了,用的却是组 ...
- 动态规划(DP),类似LIS,FatMouse's Speed
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1108 解题报告: 1.首先按照weight从小到大排列,weig ...
- 布局方式-inline-block布局
.像文本一样排block元素 .没有清除浮动等问题 .需要处理间隙 一种方式 <style> .container{ width: 800px; height: 200px; fo ...
- javascript dom 操作详解 js加强
js加强操作实现代码. 1 .文档里的每个节点都有属性 nodeName . nodeValue . nodeType nodeName 文本节点的的节点名是 #text , nodeName 是只读 ...
- 查看flash的版本
查看当前浏览器的flash版本: http://www.adobe.com/swf/software/flash/about/flashAbout_info_small.swf 针对谷歌浏览器 chr ...
- 消息中间件JMS(三)
1. Spring整合JMS 1.1消息生产者 创建工程springJMS_producer,并在pom文件中引入SpringJms .activeMQ以及单元测试相关依赖 <propertie ...
- lucene&solr学习——创建和查询索引(代码篇)
1. Lucene的下载 Lucene是开发全文检索功能的工具包,从官网下载Lucene4.10.3并解压. 官网:http://lucene.apache.org/ 版本:lucene7.7.0 ( ...