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查询语句表之间的行列转换,同时也包括如何将一行数据转换成两 ...
随机推荐
- IFrame安全问题解决办法(跨框架脚本(XFS)漏洞)
最近项目要交付了,对方安全测试的时候检测出高危险漏洞,由于刚参加工作不久,经验不足,未涉及过此方面的东西.经过一番查询和探索,最终解决了这个问题,记录一下. 发现的漏洞为缺少跨框架脚本保护.跨框架脚本 ...
- Smokeping配置调整
smokeping两种邮件报警方式 一 .自带sendmail报警 修改这两句话to = 收件邮箱,多个逗号分隔from = smokealert@本机IP /usr/local/smokeping/ ...
- Orchard Core 中文文档翻译(一)关于Orchard Core
原文连接:https://www.cnblogs.com/Qbit/p/9746363.html 转载请注明出处 翻译说明:本系列为直译,按照官方的计划现在这个版本(2018年10月5日)已经接近最终 ...
- 用C++实现HTTP服务器 - Windows平台(开放源代码)
有时间了看一下 https://blog.csdn.net/querw/article/details/6593328 libevent也实现了一下http服务
- n对mod求模整除时转化成mod的数学式
n对mod求模,它的值在0到mod-1之间,如果要求模整除的时候转化成mod可以用下面的式子: n = (n - 1 % mod + mod) % mod +1 这里先减一,模上mod再加一,这样如果 ...
- 树状数组 && 线段树应用 -- 求逆序数
参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...
- PHP面试题分享与答案
由于之前的每一个问题都是一个比较大的知识点,作者希望可以尽量一一详细解答,如果有不足的地方欢迎大家补充和修改,同时借鉴牛人写的Mysql中算法的实现以及内存原理,Btree结构等. 1:MySQL数据 ...
- Mysql之inner join,left join,right join详解
首先借用官方的解释下: inner join(等值连接):只返回两个表中联结字段相等的行: left join(左联接):返回包括左表中的所有记录和右表中联结字段相等的记录: right join(右 ...
- 代替eval执行字符串表达式
function eval2(str) { var Fn = Function; return new Fn('return ' + str)(); }
- Django-rest-framework(二)serializers 使用
简介 初次见到serializers文件,想必大家都会感到陌生,所以,我们不妨换个词来形容他的作用,那就是django 中的Form,这样是不是感觉熟悉了一点. 实际上,serializers 的作用 ...