WITH LANES AS ( SELECT MIN(timestamp) AS start_timestamp, MAX(timestamp) AS end_timestamp, 3600/TIMESTAMPDIFF(SECOND, :startDate, :endDate) AS pph_multiplier, lane_id AS induct, SUM(count) AS total FROM pe_history WHERE timestamp BETWEEN :startDate AND :endDate AND lane_id LIKE "UL%" GROUP BY lane_id ORDER BY lane_id ) SELECT start_timestamp, end_timestamp, induct, /* Counts: */ total AS total_count, /* PPH: */ ROUND(total*pph_multiplier) AS total_pph, /* Percents: */ ROUND(total/total, 4) AS total_percent FROM LANES;