滑动聚合是按顺序对滑动窗口范围内的数据进行聚合的操作。下累积聚合不同,滑动聚合并不是统计开始计算的位置到当前位置的数据。

这里以统计最近三个月中员工第月订单情况为例来介绍滑动聚合。
滑动聚合和累积聚合解决方案的主要区别在于连接的条件不同。滑动聚合条件不再是b.ordermonth <= a.ordermonth,而应该是b.ordermonth大于前三个月的月份,并且小于当前月份。因此滑动聚合的解决方案的SQL语句如下

SELECT
a.empid,
DATE_FORMAT(a.ordermonth, '%Y-%m') AS ordermonth,
a.qty AS thismonth,
SUM(b.qty) AS total,
CAST(AVG(b.qty) AS DECIMAL(5,2)) AS avg
FROM emporders a
INNER JOIN emporders b
ON a.empid=b.empid
AND b.ordermonth > DATE_ADD(a.ordermonth, INTERVAL -3 MONTH)
AND b.ordermonth <= a.ordermonth
WHERE DATE_FORMAT(a.ordermonth,'%Y')='2015' AND DATE_FORMAT(b.ordermonth,'%Y')='2015'
GROUP BY a.empid,DATE_FORMAT(a.ordermonth, '%Y-%m'),a.qty
ORDER BY a.empid,a.ordermonth
运行结果如下
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAV4AAAEbCAIAAAAd3HuSAAAgAElEQVR4nO1dy47bSJblVxkJfoF/Y9IFLvIrvC2VG+CuvkHodpWNBvgB3uRy0LbbGwHTD/d0z6AWnVXjd2UhZkEyeBk34orKFINxru8BYThDIfIcUXEUQeoeVb9/+kfbbLPNtmCrfv/0j85gMBjmQLKGX375ZWsKm0G9dkSBiJxlUEVmDRhQrx1RICJnGajW8PPPP29NYTOo144oEJGzDKrIrAED6rUjCkTkLMOsAQ/qtSMKROQso3hrOLR13R5Y883Nzdn3uQpWONa9tHPkfDWWHe7MAs+LNd6QRYIqKsAa+OueOBP//ve/z3mUs8MfYoVj3Uu7h8BwjdfnlBfkPAIX8jn10TXekEWCKhqs4ddfbz9++lLK9uffXVz87vW88fb2t3/9z//enWdsn2vRPvex7qt9fYb3PNzZBC7kc+qja7why9t6RR8+fu63wRq+/Hrrm+64vX5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WYdaAB/XaEQUicpZRsDUUnvJE+z+NlAush/LfhU0Q8UROpX+5fR/+2mUTKGc6TS2+vqCklKepff672CmGTs8XpWFSnizKZQ5y4vqqyqm2shsqsruG/F62WNizEpZkOtFShf2hxJSnUAXhvGOFHnrKqyjKTnmSPGUNlG4NBH2U03T6xvKqKeLpzjk994eY6USxGysvS0x5EgvJ40+HL8o2GAxloPr90z/aZptttgXb/wPDMvZKqLD7sQAAAABJRU5ErkJggg==" alt="" />
该解决方案返回的是三个月为一个周期的滑动聚合,但是每个用户包含前两个月并且未满3个月的聚合。如果只希望返回满3个月的聚合,不返回未满3个月的聚合,可以使用HAVING过滤器进行过滤,过滤的条件为MIN(b.ordermonth)=DATE_ADD(a.ordermonth, INTERVAL -2 MONTH),例如

SELECT
a.empid,
a.ordermonth AS ordermonth,
a.qty AS thismonth,
SUM(b.qty) AS total,
CAST(AVG(b.qty) AS DECIMAL(5,2)) AS avg
FROM emporders a
INNER JOIN emporders b
ON a.empid=b.empid
AND b.ordermonth > DATE_ADD(a.ordermonth, INTERVAL -3 MONTH)
AND b.ordermonth <= a.ordermonth
WHERE DATE_FORMAT(a.ordermonth,'%Y')='2015' AND DATE_FORMAT(b.ordermonth,'%Y')='2015' AND a.empid=1
GROUP BY a.empid,DATE_FORMAT(a.ordermonth, '%Y-%m'),a.qty
HAVING MIN(b.ordermonth)=DATE_ADD(a.ordermonth, INTERVAL-2 MONTH)
ORDER BY a.empid,a.ordermonth

运行结果如下

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWgAAADRCAIAAABuNb+LAAAapklEQVR4nO2dzW7cSLKF+VRGgU/g17iSwYWewtuu9gDc+RmEGbdlDMAH8EbLQVtubwq4Mz2eO3MvejHdPbbkHxl5FySTkRmRIVJikRnB+EAYciqLPJEsnuJP5VHxxxd/tsUWW2yZtBR/fPFnZxiGMQVJxvH777+vLSEv1A+IxAIlauYhKzLjEIz6AZFYoETNPOKN47fffltbQl6oHxCJBUrUzENWZMYhG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lgCYQupxbC4JYCANGN7IGP+k8+iacd4XbsGaoevhfDDccldF2RkHg5jjZCnUD4jEAiVq5jHj0Ib6AZFYoETNPGYc2lA/IBILlKiZx4xDG+oHRGKBEjXzcMYxKQ7IMIxNkTQOwzCM8ZhxGIYxGTMOwzAmY8ZhGMZkzDgMw5iMGYdhGJMx4zAMYzJmHIZhTOb/AZXGBltefeueAAAAAElFTkSuQmCC" alt="" />

mysql滑动聚合的更多相关文章

  1. MySQL数据库聚合函数

    +++++++++++++++++++++++++++++++++++++++++++标题:MySQL数据库聚合函数时间:2019年2月25日内容:MySQL数据库聚合函数重点:MySQL数据库聚合函 ...

  2. MySQL之聚合数据(AVG,COUNT,MAX,MIN,SUM)

    1.首先我们需要了解下什么是聚合函数 聚合函数aggregation function又称为组函数. 认情况下 聚合函数会对当前所在表当做一个组进行统计. 2.聚合函数的特点 1.每个组函数接收一个参 ...

  3. MySQL 排错-解决MySQL非聚合列未包含在GROUP BY子句报错问题

    排错-解决MySQL非聚合列未包含在GROUP BY子句报错问题 By:授客 QQ:1033553122   测试环境 win10 MySQL 5.7 问题描述: 执行类似以下mysql查询, SEL ...

  4. MySQL的聚合函数

    MySQL的聚合函数 网站:https://www.runoob.com/mysql/mysql-functions.html 函数名 功能描述 count() 计数 sum() 求和 avg() 平 ...

  5. Over:窗口函数(滑动聚合)

    Over 窗口函数在Select 子句中,对查询的结果集进行“滑动-聚合”运算:如果使用count,那么基于滑动窗口的聚合语义同 base+1 累加:如果使用sum,那么基于滑动窗口的聚合语义等同于数 ...

  6. mysql 从聚合函数group by到sql_mode

    说到group by, 想必大家都不陌生, 就是对查询的数据进行分组,我们可以通过该操作实现一些特殊需求,比如去重. 最近在项目中使用HQL:" from TSjrz where CBh = ...

  7. mysql连续聚合

    连续聚合是按时间顺序对有序数据进行聚合的操作. 在下面的救示例中将使用EmpOrders表,该表用于存放每位员工每月发生的订购数量. 运行如下 代码创建EmpOrders表并填充示例数据. CREAT ...

  8. Mysql 分组聚合实现 over partition by 功能

    mysql中没有类似oracle和postgreSQL的 OVER(PARTITION BY)功能. 那么如何在MYSQL中搞定分组聚合的查询呢 先说结论: 利用 group_concat + sub ...

  9. MySQL分组聚合group_concat + substr_index

    场景:给予一张商品售卖表,表中数据为商品的售卖记录,假设表中数据是定时脚本插入的,每个时间段的商品售卖数量不同,根据此表找各个商品的最多售卖数量的数据. 1.数据表 CREATE TABLE `goo ...

随机推荐

  1. PDF分享:国外优秀数学教材选评

    <国外优秀数学教材选评>推荐书目下载 具体内容请查看原内容: http://www.library.fudan.edu.cn/wjzx/list/373-1-20.htm 或者http:/ ...

  2. IE兼容问题 动态生成的节点IE浏览器无法触发

    ie下click()不能操作文档中没有的节点,所以你可以在click()前添加下面的语句 document.body.appendChild( input ); input.style.display ...

  3. PID control

    |—平滑化算法 |—PID控制—|—P控制器编程 |—PD控制编程 |—PID控制编程 |—参数优化 |—实验P.PD.PID对减小系统误差的作用 这里讨论怎么将路径转变成行动指令(生成平滑的路径), ...

  4. win7里linux虚拟机安装vmware tools(ubuntu12.04)

    安装Vmware Tools工具 1.安装linux虚拟机(略) 2.虚拟机去启动,选择虚拟机à设置,“硬件”中选择CD/DVD(IDE),右侧选择“使用ISO镜像文件(M)”  -- 文件选择vmw ...

  5. 小白Monkey学习笔记

    Monkey是google提供的一款对Android app进行压力测试工具,基于随机坐标位置,进行点击.滑动.输入等操作. Monkey的环境配置 pc电脑需要配置adb环境 Monkey程序由An ...

  6. nmap 介绍

    原文地址:http://drops.wooyun.org/tips/2002 原文地址:http://infotechbits.wordpress.com/2014/05/04/introductio ...

  7. 最近一个dish项目的建设思考

    系统通用能力的沉淀:a.核心模型的数据沉淀 b.通用服务能力的沉淀 ps1:以前重心主要放在了业务的抽象和通过设计模式来增加可复用的扩展性.局限在于,抽象的范围会被单个业务或者当前的业务所束缚,在更大 ...

  8. Docker构建其它组件

    构建mysql 运行centos7容器 docker run --privileged -dti --name=centos-container centos:7 /usr/sbin/init 查询c ...

  9. day_7数据类型的相互转换,与字符编码

    首先复一下昨天的内容 1:深浅拷贝 1:值拷贝 直接赋值 列表1=列表2       列表1中的任何值发生改变,列表2中的值都会随之改变 2:浅拷贝,列表2=列表1  列表1中存放的值的地址没有改变, ...

  10. Java 使用 Jxl 实现 Excel 导入导出

    开发过程中经常需要用到数据的导入导出功能,之前用的是POI,这次使用JXL,JXL相对于POI来说要轻量简洁许多,在数据量不大的情况下还是非常实用的.这里做一下使用JXL的学习记录.首先需要导入相应的 ...