-- Collection of code snippets by Arne Vajhøj 
-- (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
CREATE TABLE q (
    id INTEGER AUTO_INCREMENT PRIMARY KEY,
    t DATE
);

INSERT INTO q(t) VALUE ('2008-02-01');
INSERT INTO q(t) VALUE ('2008-02-02');
INSERT INTO q(t) VALUE ('2008-02-03');
INSERT INTO q(t) VALUE ('2008-03-01');
INSERT INTO q(t) VALUE ('2008-04-01');
INSERT INTO q(t) VALUE ('2008-05-01');
INSERT INTO q(t) VALUE ('2008-05-01');
INSERT INTO q(t) VALUE ('2008-08-01');
INSERT INTO q(t) VALUE ('2008-12-01');
INSERT INTO q(t) VALUE ('2009-01-01');
INSERT INTO q(t) VALUE ('2009-08-01');
INSERT INTO q(t) VALUE ('2009-09-01');
INSERT INTO q(t) VALUE ('2009-10-01');
INSERT INTO q(t) VALUE ('2009-11-01');
INSERT INTO q(t) VALUE ('2009-12-01');
INSERT INTO q(t) VALUE ('2010-01-01');

SELECT * FROM q;

SELECT ((YEAR(t)*12+MONTH(t)-2) DIV 3) DIV 4 AS starty,
       (((YEAR(t)*12+MONTH(t)-2) DIV 3) MOD 4)*3+2 AS startm,
       ((YEAR(t)*12+MONTH(t)-2) DIV 3 + 1) DIV 4 AS endy,
       (((YEAR(t)*12+MONTH(t)-2) DIV 3 + 1) MOD 4)*3+1 AS endm,
       COUNT(*) AS n
FROM q
GROUP BY (YEAR(t)*12+MONTH(t)-2) DIV 3;

DROP TABLE q;
