Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.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 - Fatal编程技术网

Mysql 发现负面变化并据此采取行动

Mysql 发现负面变化并据此采取行动,mysql,Mysql,我有一张桌子,我只想看看AB ID CODE COUNT 102 AB 9 101 AB 8 100 AC 23 //not important!!!! 99 AB 7 98 AB 6 97 AB 5 96 AB 0 然后我想计算代码为“AB”的特定ID之间的差异 所以 我是通过@bonCodigo的查

我有一张桌子,我只想看看AB

ID     CODE       COUNT   
102    AB         9
101    AB         8
100    AC         23    //not important!!!!
99     AB         7
98     AB         6
97     AB         5
96     AB         0
然后我想计算代码为“AB”的特定ID之间的差异

所以

我是通过@bonCodigo的查询来实现的

select ID, DIFFERENCE, 
COUNT from (
    SELECT
    t.ID, t.CODE, t.COUNT,
    @PREVCOUNT,
    @PREVCOUNT - t.COUNT DIFFERENCE,
    @PREVCOUNT := t.COUNT  -- Updates for the next iteration, so it
                           -- must come last!
    FROM
    (SELECT ID, CODE, COUNT FROM some_table WHERE CODE = 'AB' ORDER BY ID DESC) t,
    (SELECT @PREVCOUNT := NULL) _uv
    group by t.id, t.code
    )x
where x.difference >= 0
order by ID DESC;
由于我的新输入数据有时会重置计数,因此它从0开始计数,直到任何值

因此,有时我会按以下顺序获取数据:

ID        COUNT
1.        0
2.        1
3.        2
4.        7
5.        4     // which means the counter has reset to 0 and counted up to 4 again.
6.        5
现在我的查询所做的是,它只计算积极的变化,并将其视为差异

那么它的作用是:

Step 1: 1 - 0 = 1
Step 2: 2 - 1 = 1
Step 3: 7 - 2 = 5
Step 4: 4 - 7 = -3 //discarded as this difference is smaller than 0
Step 5: 5 - 4 = 1
如果我把这个加起来,我得到8

而我希望这段代码在出现负差异时从0开始计数

所以我想要的是:

Step 1: 1 - 0 = 1
Step 2: 2 - 1 = 1
Step 3: 7 - 2 = 5
Step 4: 4 - 7 = -3  BUT MAKE IT 4 because the counter started from 0 again.
Step 5: 5 - 4 = 1
所以,如果我把这个加起来,我得到12,一个if语句就可以了

IF((@PREVCOUNT - t.COUNT) < 0, @PREVCOUNT, (@PREVCOUNT - t.COUNT)) DIFFERENCE
这是工作小提琴:

修改后的查询为:

select ID, DIFFERENCE, 
COUNT from (
    SELECT
    t.ID, t.CODE, t.COUNT,
    @PREVCOUNT,
    IF((@PREVCOUNT - t.COUNT) < 0, @PREVCOUNT, (@PREVCOUNT - t.COUNT)) DIFFERENCE,
    @PREVCOUNT := t.COUNT  -- Updates for the next iteration, so it
                           -- must come last!
    FROM
    (SELECT ID, CODE, COUNT FROM some_table WHERE CODE = 'AB' ORDER BY ID DESC) t,
    (SELECT @PREVCOUNT := NULL) _uv
    group by t.id, t.code
    )x
where x.difference >= 0
order by ID DESC;

复杂而不清楚。我建议把它分成更简单的部分、部分或多个问题。对不起,下次我会尽量说得更清楚。嗨@Lazykiddy,如果有帮助,请告诉我。谢谢!这就是我需要的。
select ID, DIFFERENCE, 
COUNT from (
    SELECT
    t.ID, t.CODE, t.COUNT,
    @PREVCOUNT,
    IF((@PREVCOUNT - t.COUNT) < 0, @PREVCOUNT, (@PREVCOUNT - t.COUNT)) DIFFERENCE,
    @PREVCOUNT := t.COUNT  -- Updates for the next iteration, so it
                           -- must come last!
    FROM
    (SELECT ID, CODE, COUNT FROM some_table WHERE CODE = 'AB' ORDER BY ID DESC) t,
    (SELECT @PREVCOUNT := NULL) _uv
    group by t.id, t.code
    )x
where x.difference >= 0
order by ID DESC;