MiniDao + MYSQL 复杂SQL 客户转化周期统计

SELECT departid,departname,create_time,
SUM( IF(conversion_time = ${((startDate?substring(2,7))?replace("-",""))?number},conversion_count,0)) as 'one',
SUM( IF(conversion_time = ${((startDate?substring(2,7))?replace("-",""))?number} + 1,conversion_count,0)) as 'two',
SUM( IF(conversion_time = ${((startDate?substring(2,7))?replace("-",""))?number} + 2,conversion_count,0)) as 'three',
SUM( IF(conversion_time = ${((startDate?substring(2,7))?replace("-",""))?number} + 3,conversion_count,0)) as 'four',
SUM( IF(conversion_time = ${((startDate?substring(2,7))?replace("-",""))?number} + 4,conversion_count,0)) as 'five',
SUM( IF(conversion_time = ${((startDate?substring(2,7))?replace("-",""))?number} + 5,conversion_count,0)) as 'six',
SUM( IF(conversion_time = ${((startDate?substring(2,7))?replace("-",""))?number} + 6,conversion_count,0)) as 'seven',
SUM( IF(conversion_time > {((startDate?substring(2,7))?replace("-",""))?number} + 6,conversion_count,0)) as 'more' FROM ( SELECT org_code, parentdepartid, departid, departname, create_time, conversion_time, conversion_period, COUNT(id) AS conversion_count FROM ( SELECT t_s_depart.org_code, t_s_depart.parentdepartid, t_s_depart.id as departid, t_s_depart.departname, c.id, c.create_date AS lead_create_date, public_sea_operation_record.create_date AS customer_create_date, DATE_FORMAT( c.create_date, '%y%m' ) as create_time, DATE_FORMAT( public_sea_operation_record.create_date, '%y%m' ) AS conversion_time, PERIOD_DIFF(DATE_FORMAT(public_sea_operation_record.create_date,'%y%m'),DATE_FORMAT(c.create_date,'%y%m')) + 1 AS conversion_period FROM customer c INNER JOIN public_sea_operation_record ON c.id = public_sea_operation_record.obj_id AND public_sea_operation_record.operation_type = 5 AND c.is_deleted = 0 AND c.stage = 2 <#-- 操作时间:范围 --> <#if startDate?? && startDate?has_content> <#--{startDate?string["yyyy-MM-dd HH:mm:ss"]}-->
AND c.create_date >= :startDate
AND c.create_date <= CONCAT(LAST_DAY(DATE_FORMAT(:startDate ,'%Y-%m-%d')),' 23:59:59')
</#if>
<#if endDate?? && endDate?has_content>
<#-- {endDate?string["yyyy-MM-dd HH:mm:ss"]}--> AND c.create_date <= :endDate </#if> <#-- 操作部门:关联部门 --> <#if ( deptIdList )?? && deptIdList?size gt 0 > INNER JOIN crm_user_customer ON crm_user_customer.obj_id = c.id AND crm_user_customer.priority = 1 INNER JOIN t_s_base_user ON crm_user_customer.member_id = t_s_base_user.id <#-- 关联用户状态--> <#if userStatus?? && userStatus?has_content> AND t_s_base_user.status = :userStatus </#if> INNER JOIN t_s_depart ON t_s_base_user.departid = t_s_depart.id AND t_s_depart.id IN ({DaoFormat.getInStrs ( deptIdList )} )
<#else>
<#-- 关联用户状态-->
<#if userStatus?? && userStatus?has_content>
INNER JOIN crm_user_customer ON crm_user_customer.obj_id = c.id AND crm_user_customer.priority = 1
INNER JOIN t_s_base_user ON crm_user_customer.member_id = t_s_base_user.id
AND t_s_base_user.status = :userStatus
</#if>
</#if>
) customer_conversion
GROUP BY
departid,
create_time,
conversion_time,
conversion_period
) customer_conversion_count
GROUP BY departid,create_time
ORDER BY org_code,parentdepartid,departname

posted @   任国强  阅读(304)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-06-03 删除 mysql-bin.00000x MySQL -- 关闭 binlog
2019-06-03 JDBC及Hibernate 执行原始SQL语句
2019-06-03 Ubuntu下查看linux版本,内核版本,系统位数,gcc版本
2019-06-03 Mysql中join后on、where的区别
2019-06-03 阿里云 WAF 前置条件
点击右上角即可分享
微信分享提示