There is no such function in the "standard SQL functions", but you can get the desired result using some tricks.
In the subquery shown below, we create a virtual field that you can use for GROUP BY
in an external query. The value of this virtual field increases every time there is a space in the oID
sequence. Thus, we create an identifier for each of these "data islands":
SELECT SUM(Area), COUNT(*) AS Count_Rows FROM ( SELECT @group_enumerator := @group_enumerator + (@prev_oID != oID - 1) AS group_enumerator, @prev_oID := oID AS prev_oID, sample_table.* FROM ( SELECT @group_enumerator := 0, @prev_oID := -1 ) vars, sample_table ORDER BY oID ) q GROUP BY group_enumerator
Test table and data generation:
CREATE TABLE sample_table (oID INT auto_increment, Area INT, PRIMARY KEY(oID)); INSERT INTO sample_table (oID, Area) VALUES (1,5), (2,2), (3,3), (5,3), (6,4), (7,5);
I need to thank Kwasnui for pointing out this trick in my related question ; -)
UPDATE: added test pattern and data and fixed duplicate column name in query example.
source share