B. SELECT *,
(
SELECT avg (t1. ` close `) from (
SELECT t. *, (@ rownum:=@ rownum + 1) AS rownum FROM (SELECT @ rownum:=0) r, ` TMPtable ` t ORDER BY ` name `, ` index ` asc) t1
WHERE a t1. Rownum & lt;=b.r ownum + 5 and t1. Rownum & gt; B.r ownum
) avg5
The from
(select *, b. (@ rownum:=@ rownum + 1) AS rownum FROM (select @ rownum:=0) r, ` TMPtable ` b ORDER BY ` name `, ` index ` asc) b
Calculated before:
After the calculation:
CodePudding user response:
Objective: according to the name column group calculating the average of 5 days prior to the close columnCodePudding user response:
Good complex SQL, didn't look too understandMy idea is like this:
To remove the time before the first five days of data, and then grouped according to the name average, but to see your data requirements:
1. A data every day?
SELECT the name, avg (close)
The FROM TMPtable
WHERE the index & gt;=SUBDATE (CURDATE (), INTERVAL 5 DAY)
GROUP BY name