Fluffy
Fluffy

Reputation: 28362

Mysql - how to group values of a column into min-max ranges?

Can I group values of an another column into multiple range definitions?

Here's an example table:

mysql> select * from t;
+------+------+
| x    | y    |
+------+------+
|    1 |    1 |
|    2 |    1 |
|    3 |    1 |
|    4 |    2 |
|    5 |    2 |
|    6 |    2 |
|    7 |    1 |
|    8 |    1 |
|    9 |    1 |
+------+------+

I want to select the following info: y=1 has ranges of x: 1-3, 7-9, y=2 has ranges: 4-6.

Definition and data:

create table t (x int, y int);
insert into t(x,y) values (1,1),(2,1),(3,1),(4,2),(5,2),(6,2),(7,1),(8,1),(9,1);

Upvotes: 3

Views: 254

Answers (2)

Carsten Massmann
Carsten Massmann

Reputation: 28196

Try this:

SELECT y,group_concat(xr) FROM (
 SELECT y,
  CASE WHEN min(x)=max(x) THEN CAST(min(x) as char(8))
       ELSE concat(CAST(min(x) AS char(8)),'-',CAST(max(x) AS char(8)))
  END xr, g FROM (
  SELECT y,x, CASE WHEN @g=@c AND x=@l+1
                   THEN @g ELSE @c:=@g:=@g+1 END g ,
         @l:=x  lastx 
  FROM tab,( SELECT @g:=0, @c:=-1, @l=-1 ) vars
  ORDER BY y,x
 ) grp1
 GROUP BY y,g
) grp2 GROUP BY y

variables used:

@g  group counter
@c  current x value
@l  previous ('last') x value

I also fixed the single items per group problem.

See here for a working fiddle: http://www.sqlfiddle.com/#!9/cc836/2

Upvotes: 1

Strawberry
Strawberry

Reputation: 33945

If x is a contiguous sequence (no gaps), then you can do this:

SELECT a.y
     , a start
     , MIN(c.x) end 
  FROM t a
  LEFT 
  JOIN t b 
    ON b.y = x.y
   AND b.x = a.x - 1
  LEFT 
  JOIN tbl c 
    ON c.y = a.y
   AND c.x >= a.x
  LEFT 
  JOIN t d 
    ON d.y = a.y
   AND d.x = c.x + 1
 WHERE b.x IS NULL 
   AND c.x IS NOT NULL
   AND d.x IS NULL
 GROUP 
    BY a.y,a.x; 

http://www.sqlfiddle.com/#!9/26aef/33

If it's not then you can do something like this:

SELECT a.y
     , a.x start
     , MIN(c.x) end 
  FROM (SELECT *,@a:=@a+1 rank FROM t,(SELECT @a:=0) vars ORDER BY x) a
  LEFT 
  JOIN (SELECT *,@b:=@b+1 rank FROM t,(SELECT @b:=0) vars ORDER BY x) b 
    ON b.y = a.y
   AND b.rank = a.rank - 1
  LEFT 
  JOIN (SELECT *,@c:=@c+1 rank FROM t,(SELECT @c:=0) vars ORDER BY x) c 
    ON c.y = a.y
   AND c.rank >= a.rank
  LEFT 
  JOIN (SELECT *,@d:=@d+1 rank FROM t,(SELECT @d:=0) vars ORDER BY x) d 
    ON d.y = a.y
   AND d.rank = c.rank + 1
 WHERE b.x IS NULL 
   AND c.x IS NOT NULL
   AND d.x IS NULL
 GROUP 
    BY a.y,a.x; 

http://www.sqlfiddle.com/#!9/26aef/54

However, both of these queries will be slow for larger data sets. That said, an idea contained within the second solution can be adapted for cases where performance is an issue - well, similar to cars10's solution.

Upvotes: 1

Related Questions