在MySQL中以某个值选择连续的行

时间:2018-11-07 10:56:05

标签: mysql sql group-by mysql-8.0

选择连续的行,且列的值小于一定值

我有一个包含以下数据的表:

crashID     crash
-----------------------
1           189
2           144
3           8939        
4           748
5           988
6           102
7           392
8           482
9           185
10          101

我想选择连续行中最长的连胜,这些连败也具有低于某个阈值的崩溃值。假设这个例子是500。

如何在单个MySQL查询中执行此操作? (v8.0.1)

所需的输出为:

crashID     crash
------------------
6           102
7           392
8           482
9           185
10          101

2 个答案:

答案 0 :(得分:0)

您可以尝试使用间隙和孤岛方法来解决它,假设每一次崩溃lte 500都是一个孤岛,然后找到最大的孤岛:

SET @threshold = 500;
WITH cte1 AS (
    SELECT
        crashID,
        CASE WHEN crash <= @threshold THEN 1 ELSE 0 END AS island,
        ROW_NUMBER() OVER (ORDER BY crashID) rn1,
        ROW_NUMBER() OVER (PARTITION BY CASE WHEN crash <= @threshold THEN 1 ELSE 0 END ORDER BY crashID) rn2
    FROM t
), cte2 AS (
    SELECT MIN(crashID) AS fid, MAX(crashID) AS tid
    FROM cte1
    WHERE island = 1
    GROUP BY rn1 - rn2
    ORDER BY COUNT(*) DESC
    LIMIT 1
)
SELECT *
FROM t
WHERE crashID BETWEEN (SELECT fid FROM cte2) AND (SELECT tid FROM cte2);

DB Fiddle

答案 1 :(得分:0)

这是旧版本MySQL的一种方法... 此解决方案假定与第一名没有关系...

SELECT m.* 
  FROM my_table m
  JOIN 
     ( SELECT MIN(crash_id) range_start
            , MAX(crash_id) range_end
         FROM 
            ( SELECT x.*
                   , CASE WHEN FLOOR(crash/500) * 500 = 0 AND @prev = FLOOR(crash/500) * 500 THEN @i:=@i ELSE @i:=@i+1 END i
                   , @prev:=FLOOR(crash/500)*500 prev 
                FROM my_table x
                   , (SELECT @prev:=null,@i:=0) vars 
               ORDER 
                  BY crash_id
            ) a
        GROUP
           BY i
        ORDER
           BY COUNT(*) DESC LIMIT 1
     ) n
    ON m.crash_id BETWEEN n.range_start AND n.range_end;