I have a table with the following structure:
CREATE TABLE `Rings` (
ID_RingType CHAR(2),
Number MEDIUMINT UNSIGNED,
ID_User INT(11)
);
and with data:
INSERT INTO `Rings` VALUES
('AA',1,1),
('AA',2,1),
('AA',3,1),
('AA',11,1),
('AA',12,1),
('AA',13,1),
('AA',14,1),
('AA',15,1),
('AB',16,1),
('AB',17,1),
('AB',18,1),
('AB',19,1),
('AB',20,2),
('AB',21,2),
('AB',22,2);
I wish to group the data based on ID_User
and ID_RingType
and for each contiguous range of numbers list the MIN and MAX.
The results should look like:
ID_User | ID_RingType | MIN | MAX
1 | 'AA' | 1 | 3
1 | 'AA' | 11 | 15
1 | 'AB' | 16 | 19
2 | 'AB' | 20 | 22
I went through several posts on this topic but was not able to tweak them to fit my data.
Any help would be appreciated.
Best Answer
The answer with the variables is going to be more efficient but here is an answer with pure SQL:
Efficiency will depend on many factors (mainly distibution of data) but an index on
(id_user, id_ringtype, number)
is essential for this query.