这是报表原型,在这张报表中,使用了动态的列与动态查询参数,动态列与动态查询参数全部使用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. 常见WEB开发安全漏洞 原因分析及解决

    目 录 1 会话标识未更新 3 1.1 原因 3 1.2 解决 3 2 SQL注入 3 2.1 原因 3 2.2 解决 5 3 XSS跨站脚本编制 5 3.1 原因 5 3.2 解决 5 4 XSRF ...

  2. 【Leetcode】【Easy】Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  3. Oracle 12C配置EM

    12C配置OEM同之前的版本差别较大,没有了emctl,而是直接使用如下方法配置: SQL*Plus: Release 12.1.0.2.0 Production on Tue Jul 19 07:1 ...

  4. [ZJOI2012]小蓝的好友

    https://www.luogu.org/problemnew/show/P2611 题解 \(n\times m\)肯定过不去.. 我们把给定的点看做障碍点,考虑先补集转化为求全空矩阵. 然后我们 ...

  5. SQL Server的Bug

    SQL 语句如下: SELECT * FROM Production.Product WHERE ProductNumber IN(SELECT ProductNumber FROM HumanRes ...

  6. 【luogu P1494 [国家集训队]小Z的袜子】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1494 #include <cstdio> #include <algorithm> ...

  7. php如何实现登陆后返回原页面

    访问网站页面时,有的页面需要授权才能访问,这时候就会要求用户登录,跳转到登录页面login.php,怎么实现登录后返回到刚才访问的页面项目需求 访问网站页面时,有的页面需要授权才能访问,这时候就会要求 ...

  8. mysql的子查询in()操作及按指定顺序显示

    代码示例: in(逗号分隔开的值列表) 释:是否存在于值列表中 --------------------- 示例: select * from test where id in(3,1,5) orde ...

  9. Restframework的版本及分页

    1.版本 1.1基于url的get传参方式 1.创建django项目(起名我的是version),再创建一个app01应用 创建完成,通过python3 manage.py startapp api ...

  10. element 列表中已选的标记

    //表格列表中已选的标记initSelFn(data){ let listData = []      listData = data.content ? data.content : []; let ...