Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 移动平均计算_Mysql_Sql_Join_Subquery - Fatal编程技术网

Mysql 移动平均计算

Mysql 移动平均计算,mysql,sql,join,subquery,Mysql,Sql,Join,Subquery,我如何编辑下面查询中的ON运算符部分,以便我希望当前代码在id=t1.id-2和其中idi获得了第一部分(id)的正确数据 -------------------------- id | A | B | E | -------------------------- 1 | NULL | NULL |NULL| -------------------------- 2 | 4 | 6 |NULL| --------------------------

我如何编辑下面查询中的ON运算符部分,以便我希望当前代码在id=t1.id-2和其中
idi获得了第一部分(id)的正确数据
--------------------------
id  | A      | B    | E  |
--------------------------
1   |  NULL  | NULL |NULL|
--------------------------
2   |  4     | 6    |NULL|
--------------------------
3   |  6     | 9    |NULL|
--------------------------
Update t  join 
    (SELECT t1.id ,ifnull(t1.A/AVG(t2.A),0) C ,ifnull(t1.B/AVG(t2.B),0) D
    FROM    t t1
    JOIN    t t2
    ON      t2.id <= t1.id
    group by t1.id ) AS tt
    on(t.id = tt.id)
    SET E = (tt.C + tt.D)/2;
Update t  join 
(
SELECT t1.id ,ifnull(t1.A/AVG(t2.A),0) C ,ifnull(t1.B/AVG(t2.B),0) D
FROM    t t1
JOIN    t t2
ON     case when t2.id < 15 then t2.id <= t1.id else t2.id=t1.id>=t1.id-2 and <=t1.id     end
group by t1.id 
) tt on(t.id = tt.id)
SET E = (tt.C + tt.D)/2;