如何获取值高于之前值的所有记录。 例如,下表中的第一个范围从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 ...
答案 0 :(得分:2)
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)
关键在于:每次出现一个新的组号时,都会告诉您下一个值。
CAVEAT :如果prev
和open
之间存在一些浮点问题,则第二个查询不是完美的解决方案。如果这些彼此非常接近,那可能就不对了。这是编写存储过程之外的最佳尝试。
答案 1 :(得分:0)
可以使用此查询对范围进行编号:
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 |
答案 2 :(得分:0)
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
。