我有一个看起来像这样的表:
----------------------
| DateTime | Value |
----------------------
| 2010-01-01 | 26 |
| 2010-02-01 | 24 |
| 2010-03-01 | 23 |
| 2010-04-01 | 28 |
| 2010-05-01 | 30 |
我需要找出连续三个月的价值是否有所增加。
如何让SQL检查每个值是否增加了? 如果有人能给我任何解决这个问题的指示,我将不胜感激!
由于
答案 0 :(得分:2)
@n
代表您要检查的连续值的数量少于2(在您的情况下为2):
SELECT MIN(increase) AS increasing
FROM (SELECT t1.`Value` > t2.`Value` AS increase
FROM tbl AS t1
JOIN tbl AS t2
ON t1.`DateTime` = t2.`DateTime` + INTERVAL 1 month
WHERE t1.DateTime < '2010-06-01'
ORDER BY t1.`DateTime` DESC
LIMIT @n) AS tmp;
或:
SELECT MIN(t1.`Value` > t2.`Value`) AS increasing
FROM tbl AS t1
JOIN tbl AS t2
ON t1.`DateTime` = t2.`DateTime` + INTERVAL 1 month
WHERE t1.`DateTime` > NOW() - INTERVAL 3 month;