Mysql
 sql >> Database >  >> RDS >> Mysql

Come raggruppare intervalli continui usando MySQL

MySQL non supporta le funzioni analitiche, ma puoi emulare tale comportamento con user- variabili definite :

SELECT   CatID, Begin, MAX(Date) AS End, Rate
FROM (
  SELECT   my_table.*,
           @f:=CONVERT(
             IF(@c<=>CatId AND @r<=>Rate AND DATEDIFF(Date, @d)=1, @f, Date), DATE
           ) AS Begin,
           @c:=CatId, @d:=Date, @r:=Rate
  FROM     my_table JOIN (SELECT @c:=NULL) AS init
  ORDER BY CatId, Rate, Date
) AS t
GROUP BY CatID, Begin, Rate

Guardalo su sqlfiddle .