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查询语句表之间的行列转换,同时也包括如何将一行数据转换成两 ...
随机推荐
- PHP腾讯与百度坐标转换
function coordinate_switch($a,$b){//百度转腾讯坐标转换 $a = Latitude , $b = Longitude $x = (double)$b - 0.006 ...
- bind 详解
请看我的有道云笔记: http://note.youdao.com/noteshare?id=eaf4194473cf4294776fbc263ffe6b89&sub=5CB214C594E0 ...
- 如何清理Windows缩略图缓存?
从Win95的IE4开始就有缩略图缓存了,缩略图缓存作用主要是用于存储Windows资源管理器缩略图图像文件.这加快了图像的显示速度,因为每次用户查看文件夹时都不需要重新生成这些较小的图像.缩略图缓存 ...
- Java Android 32位16位 MD5加密
// md5加密 32位小写 private String Md5(String sourceStr) { String result = ""; try { MessageDig ...
- bzoj2003 [Hnoi2010]矩阵
Description Input 第一行包含三个正整数N M P表示矩阵的行数列数以及每个数的范围,接下来N行每行包含M个非负整数,其中第i行第j个数表示以格子(i,j)为右下角的2*2子矩阵中的数 ...
- 【[ZJOI2008]骑士】
这道题好暴力啊 发现自己刚学\(OI\)的时候对着这道题写了一个大搜索 发现已经看不懂了 果然我现在菜到连一年半前的我都不如了 这其实是一个基环树\(dp\)啦,基环树上的最大点独立集 其实很简单,我 ...
- hack-checkbox
checkbox选择按钮要用我们自己的样式,看到这个的时候,很可能会以为需要checkbox才能实现,用css可能很难.其实狠简单. <style> .checkbox input{ di ...
- data-ng-hide 指令
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 关于ProjectServer调用PSI 报Error GeneralReadOnlyColumn (20005) - column TS_ACT_FINISH_DATE错的解决方案
TimesheetDataSet Table Actuals Row: TS_LINE_UID='f4b970f8-fb03-44d1-9997-cd31da42cb09' TS_ACT_START_ ...
- js邮箱验证,身份证验证,正则表达式
邮箱验证: html部分: 邮箱验证:<input type="text" id="mail" value="" / onkeyup= ...