GROUP BY and aggregate sequential numeric values

Identifying non-consecutive values is always a bit tricky and involves several nested sub-queries (at least I cannot come up with a better solution). The first step is to identify non-consecutive values for the year: Step 1) Identify non-consecutive values select company, profession, year, case when row_number() over (partition by company, profession order by year) = … Read more

Get a list of dates between two dates

I would use this stored procedure to generate the intervals you need into the temp table named time_intervals, then JOIN and aggregate your data table with the temp time_intervals table. The procedure can generate intervals of all the different types you see specified in it: call make_intervals(‘2009-01-01 00:00:00′,’2009-01-10 00:00:00′,1,’DAY’) . select * from time_intervals . … Read more

MySQL how to fill missing dates in range?

MySQL doesn’t have recursive functionality, so you’re left with using the NUMBERS table trick – Create a table that only holds incrementing numbers – easy to do using an auto_increment: DROP TABLE IF EXISTS `example`.`numbers`; CREATE TABLE `example`.`numbers` ( `id` int(10) unsigned NOT NULL auto_increment, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; Populate the table … Read more