这是报表原型,在这张报表中,使用了动态的列与动态查询参数,动态列与动态查询参数全部使用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报表,动态列与查询参数+行列转换的更多相关文章

  1. MyBatis 实践 -动态SQL/关联查询

    MyBatis 实践 标签: Java与存储 动态SQL 动态SQL提供了对SQL语句的灵活操作,通过表达式进行判断,对SQL进行拼接/组装. if 对查询条件进行判断,如果输入参数不为空才进行查询条 ...

  2. MyBatis的动态SQL操作--查询

    查询条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL,即根据不同的情况生成不同的sql语句. 模拟一个场景,在做多条件搜索的时候,

  3. SQL行列转换6种方法

    在进行报表开发时,很多时候会遇到行列转换操作,很对开发人员针对于SQL级别行列转换操作一直不甚理解,今天正好抽空对其进行了一些简单的总结.这里主要列举3种可以实现SQL行列转换的方法,包括通用SQL解 ...

  4. KingbaseES 行列转换函数

    关键字:    行专列,列转行, pivot, unpivot 行列转换是在数据分析中经常用到的一项功能,KingbaseES从V8R6C3B0071版本开始通过扩展插件(kdb_utils_func ...

  5. Dynamic CRM 2013学习笔记(二十六)报表设计:Reporting Service报表 动态参数、参数多选全选、动态列、动态显示行字体颜色

    上次介绍过CRM里开始报表的一些注意事项:Dynamic CRM 2013学习笔记(十五)报表入门.开发工具及注意事项,本文继续介绍报表里的一些动态效果:动态显示参数,参数是从数据库里查询出来的:参数 ...

  6. 8.mybatis动态SQL模糊查询 (多参数查询,使用parameterType)

    多参数查询,使用parameterType.实例: 用户User[id, name, age] 1.mysql建表并插入数据 2.Java实体类 public class User { public ...

  7. 利用MyBatis的动态SQL特性抽象统一SQL查询接口

    1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...

  8. mybatis的mapper特殊字符转移以及动态SQL条件查询

    前言 我们知道在项目开发中之前使用数据库查询,都是基于jdbc,进行连接查询,然后是高级一点jdbcTemplate进行查询,但是我们发现还是不是很方便,有大量重复sql语句,与代码偶合,效率低下,于 ...

  9. [数据库] SQL查询语句表行列转换及一行数据转换成两列

    原文来自:http://blog.csdn.net/Eastmount/article/details/50559008 本文主要讲述了SQL查询语句表之间的行列转换,同时也包括如何将一行数据转换成两 ...

随机推荐

  1. Python新式类 单例模式与作用域(四)

    1 新式类与旧式类 新式类拥有经典类的全部特性之外,还有一些新的特性,比如 __init__发生变化,新增了静态方法__new__,python3目前都采用新式类,新式类是广度优先,旧式类是深度优先 ...

  2. React学习笔记 - 组件&Props

    React Learn Note 4 React学习笔记(四) 标签(空格分隔): React JavaScript 三.组件&Props 组件可以将UI切分成一些独立的.可复用的部件,这样你 ...

  3. CSS中的EM属性之弹性布局

    这篇教程将引导大家如何使用“em”来创建一个基本的弹性布局,从而学习其如何计算?又是如何使用“em”对层进行弹性扩展?又是如何扩展文本和图像等内容?下在我们就一起带着这些问题开始今天的“em”之行. ...

  4. Uva 10294 Polya

    #include <bits/stdc++.h> using namespace std; typedef long long LL; int gcd(int a,int b) { ? a ...

  5. 记一次MySQL手工注入

    本来想找个装安全狗的站试下绕过,safe dog没找到,但随便一搜搜到一个小站有SQLi,正好借此机会复习下手工注入(新版Firefox我吐槽一下,hackbar这么好用的工具,说阉割就阉割,哎) 小 ...

  6. Caffe计算net、layer向前向后传播时间

    在caffe中计算某个model的整个net以及各个layer的向前向后传播时间,可以使用下面的命令格式: ./build/tools/caffe time --model=examples/mnis ...

  7. HDU 1077 Catching Fish(用单位圆尽可能围住多的点)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1077 Catching Fish Time Limit: 10000/5000 MS (Java/Oth ...

  8. bootstrapTable 问题

    bootstrapTable引用问题 $("#table").bootstrapTable({ // 对应table标签的id method: 'post', url: 'abc' ...

  9. 小白袍 -- Chapter 1 Java中的Encode与Decode

    前几天做一个邮件发送功能,一些常用信息配置在properties文件中,通过prop.getProperty(key)来获取配置的信息,结果配置文件中是用中文写的,邮件发送成功后,邮箱中的激活链接是乱 ...

  10. 配置隐私协议 - iOS

    根据苹果隐私协议新规的推出,要求所有应用包含隐私保护协议,故为此在 App 中添加了如下隐私协议模块. 首次安装 App 的情况下默认调用隐私协议模块展示相关信息一次,当用户点击同意按钮后,从此不再执 ...