the best thing I have to propose is a Stored Procedure (if you MySQL engine allows them)
DELIMITER $$
DROP PROCEDURE IF EXISTS `getTrailAndLive` $$
CREATE PROCEDURE `getTrailAndLive` ()
BEGIN
DECLARE i INTEGER;
CREATE TEMPORARY TABLE `liveTable` (
`liveMonthId` INTEGER UNSIGNED NOT NULL,
`liveValue` INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (`liveMonthId`)
);
SET i = 1;
labelLoop: LOOP
INSERT INTO `liveTable` SELECT i, COUNT(*) FROM `myTable` WHERE MONTH(startdate) < i;
SET i = i + 1;
IF(i < 13) THEN ITERATE labelLoop;
END IF;
LEAVE labelLoop;
END LOOP labelLoop;
SELECT MONTH(startdate) AS MonthId, COUNT(*) AS Trail, liveValue AS Live
FROM `myTable`, `liveTable`
WHERE liveMonthId=MONTH(startdate)
GROUP BY MONTH(startdate);
DROP TABLE IF EXISTS `liveTable`;
END $$
DELIMITER ;
PierrOz
2010-09-03 12:00:57