如何获取值高于先前值的所有记录。例如,下表中的第一个范围从 id 1 开始到 id 6 结束,下一个范围是从 7 到 10,等等...
id Open
1 1.30077
2 1.30088
3 1.30115
4 1.30132
5 1.30135
6 1.30144
7 1.30132
8 1.30137
9 1.30152
10 1.30158
11 1.30149
12 ...
USE test
DROP TABLE IF EXISTS rangedata;
CREATE TABLE rangedata
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
open FLOAT
) ENGINE=MyISAM;
INSERT INTO rangedata (open) VALUES
(1.30077),(1.30088),(1.30115),(1.30132),
(1.30135),(1.30144),(1.30132),(1.30137),
(1.30152),(1.30158),(1.30149),
(1.30077),(1.30088),(1.30115),(1.30132),
(1.30135),(1.30144),(1.30132),(1.30137),
(1.30152),(1.30158),(1.30149),
(1.30077),(1.30088),(1.30115),(1.30132),
(1.30135),(1.30144),(1.30132),(1.30137),
(1.30152),(1.30158),(1.30149);
mysql> USE test
Database changed
mysql> DROP TABLE IF EXISTS rangedata;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE rangedata
-> (
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> open FLOAT
-> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0.09 sec)
mysql> INSERT INTO rangedata (open) VALUES
-> (1.30077),(1.30088),(1.30115),(1.30132),
-> (1.30135),(1.30144),(1.30132),(1.30137),
-> (1.30152),(1.30158),(1.30149),
-> (1.30077),(1.30088),(1.30115),(1.30132),
-> (1.30135),(1.30144),(1.30132),(1.30137),
-> (1.30152),(1.30158),(1.30149),
-> (1.30077),(1.30088),(1.30115),(1.30132),
-> (1.30135),(1.30144),(1.30132),(1.30137),
-> (1.30152),(1.30158),(1.30149);
Query OK, 33 rows affected (0.00 sec)
Records: 33 Duplicates: 0 Warnings: 0
mysql>
这是 LEFT JOIN 查询
SET @grp = 1;
SELECT A.open prev,(@grp:=@grp+IF(A.open<B.open,1,0)) group_number
FROM rangedata A LEFT JOIN rangedata B ON A.id= B.id+1;
这是它的输出
mysql> SELECT A.open prev,(@grp:=@grp+IF(A.open<B.open,1,0)) group_number
-> FROM rangedata A LEFT JOIN rangedata B ON A.id= B.id+1;
+---------+--------------+
| prev | group_number |
+---------+--------------+
| 1.30088 | 1 |
| 1.30115 | 1 |
| 1.30132 | 1 |
| 1.30135 | 1 |
| 1.30144 | 1 |
| 1.30132 | 2 |
| 1.30137 | 2 |
| 1.30152 | 2 |
| 1.30158 | 2 |
| 1.30149 | 3 |
| 1.30077 | 4 |
| 1.30088 | 4 |
| 1.30115 | 4 |
| 1.30132 | 4 |
| 1.30135 | 4 |
| 1.30144 | 4 |
| 1.30132 | 5 |
| 1.30137 | 5 |
| 1.30152 | 5 |
| 1.30158 | 5 |
| 1.30149 | 6 |
| 1.30077 | 7 |
| 1.30088 | 7 |
| 1.30115 | 7 |
| 1.30132 | 7 |
| 1.30135 | 7 |
| 1.30144 | 7 |
| 1.30132 | 8 |
| 1.30137 | 8 |
| 1.30152 | 8 |
| 1.30158 | 8 |
| 1.30149 | 9 |
| 1.30077 | 9 |
+---------+--------------+
33 rows in set (0.01 sec)
使用用户定义的变量,您只需监视每一行并查看前一个值何时更大。准备好查询了吗?这里是:
SET @prev = '0.00000';
SET @grp = 1;
SELECT id,open,(@grp:=@grp+increasing) group_number FROM
(SELECT id,open,IF(@prev<=open,0,1) increasing,(@prev:=open) FROM rangedata) A;
这是您的样本数据增加了两倍:
这是查询的执行:
mysql> SET @prev = '0.00000';
Query OK, 0 rows affected (0.00 sec)
mysql> SET @grp = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT id,open,(@grp:=@grp+increasing) group_number FROM
-> (SELECT id,open,IF(@prev<=open,0,1) increasing,(@prev:=open) FROM rangedata) A;
+----+---------+--------------+
| id | open | group_number |
+----+---------+--------------+
| 1 | 1.30077 | 1 |
| 2 | 1.30088 | 1 |
| 3 | 1.30115 | 1 |
| 4 | 1.30132 | 1 |
| 5 | 1.30135 | 1 |
| 6 | 1.30144 | 1 |
| 7 | 1.30132 | 2 |
| 8 | 1.30137 | 2 |
| 9 | 1.30152 | 2 |
| 10 | 1.30158 | 2 |
| 11 | 1.30149 | 3 |
| 12 | 1.30077 | 4 |
| 13 | 1.30088 | 4 |
| 14 | 1.30115 | 4 |
| 15 | 1.30132 | 4 |
| 16 | 1.30135 | 4 |
| 17 | 1.30144 | 4 |
| 18 | 1.30132 | 5 |
| 19 | 1.30137 | 5 |
| 20 | 1.30152 | 5 |
| 21 | 1.30158 | 5 |
| 22 | 1.30149 | 6 |
| 23 | 1.30077 | 7 |
| 24 | 1.30088 | 7 |
| 25 | 1.30115 | 7 |
| 26 | 1.30132 | 7 |
| 27 | 1.30135 | 7 |
| 28 | 1.30144 | 7 |
| 29 | 1.30132 | 8 |
| 30 | 1.30137 | 8 |
| 31 | 1.30152 | 8 |
| 32 | 1.30158 | 8 |
| 33 | 1.30149 | 9 |
+----+---------+--------------+
33 rows in set (0.00 sec)
关键点是:每次出现一个新的组号时,都会告诉您下一个丢弃的值。
CAVEATprev
:第二个查询不是一个完美的解决方案,以防和之间存在一些浮点问题open
。如果它们彼此离得太近,那可能是不对的。这是编写存储过程之外的最佳尝试。
可以使用此查询对范围进行编号:
SELECT id, open, range_number
FROM(
SELECT *,
if(@lastopen<open,@grp,@grp:=@grp+1) range_number,
@lastopen:=open
FROM table1,
(select @lastopen:=null,@grp:=0) qqq
ORDER BY id
) qqq;
演示:http ://www.sqlfiddle.com/#!2/b1bb8/9
| ID | OPEN | RANGE_NUMBER |
|----|----------------|--------------|
| 1 | 1.300770044327 | 1 |
| 2 | 1.300879955292 | 1 |
| 3 | 1.301149964333 | 1 |
| 4 | 1.301319956779 | 1 |
| 5 | 1.30134999752 | 1 |
| 6 | 1.301440000534 | 1 |
| 7 | 1.301319956779 | 2 |
| 8 | 1.301370024681 | 2 |
| 9 | 1.301519989967 | 2 |
| 10 | 1.30157995224 | 2 |
| 11 | 1.301489949226 | 3 |
SELECT startid,MAX(id) FROM (
SELECT
@currentid := IF(@previous <= open,@currentid,id) as startid,
@previous := open,
id
FROM ranges
JOIN (SELECT @currentid := MIN(id), @previous := MIN(open) FROM ranges) as variables
ORDER BY id) runningscan
GROUP BY startid ORDER BY startid + 0;
在 SQLFiddle 上看到这个:http ://sqlfiddle.com/#!2/e3cea/3
它的作用是:在 runningscan 子查询中,它遍历表一次,如果open
大于或小于前一个open
(存储在@previous
变量中),则保持点击。这将为您提供一个包含所有 id 的列表,以及开始中断更高的 id (或等于)'run'。由此,我们只需要找到一个起始id的最高id,所以我们把它放在一个子查询中以便于max
构造。id
列中的间隙没有问题。如果你单行可以'不是一个范围(即:open
连续降低两次或更多),WHERE startid < id
在外部查询中添加一个子句。如果您需要一个最小行数的范围来限定一个大于1的范围,请添加一个HAVING COUNT(*) > your_desired_minimum
。