Year-to-date aggregation and sliding aggregation are similar, except that only the aggregations of the current year are counted. The only difference is in the starting position of the lower bound. In the year-to-date problem, the lower bound is the first day of the years, and the lower bound of the sliding aggregate is the first day of n months. So the solution to the problem of the year-to-date is as shown in the results obtained
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 Avgfrom emporders a INNER JOIN emporders B on a.empid=b.empid and b . OrderMonth >= Date_format (a.ordermonth, '%y-01-01 ') and b.ordermonth <= a.ordermonth and DATE_FORMAT ( B.ordermonth, '%Y ') = ' + ' GROUP by A.empid,a.ordermonth,a.qtyorder by A.empid,a.ordermonth
The operation results are as follows
MySQL converged since the beginning of the year