我有一张桌子:
id firstval secondval
1 4 5
2 5 4
3 3 3
4 6 6
5 7 8
6 9 8
7 3 3
8 3 3
我需要做的第一件事是计算秒数>的次数. firstval.这显然没问题.
然而,我正在努力的是如何计算下一行满足条件secondval firstval的每个实例)的次数. firstval
所以在这个例子中有两行满足第一个规则id 1& 5和2为第二个规则,下一行为id 2和6.
最佳答案
SELECT id, @prevGreater AND secondval < firstval AS discrepancy,
@prevGreater := secondval > firstval AS secondGreater
FROM (SELECT * FROM YourTable ORDER BY id) AS x
CROSS JOIN (SELECT @prevGreater := false) AS init