WITH INDUCTS AS ( SELECT DATE_FORMAT(MIN(s04_timestamp), "%Y-%m-%d %H:%i:00") AS time, sorter, induct, COUNT(*) AS total, SUM(carriers=1) AS single_carrier, SUM(carriers=2) AS double_carrier FROM package_history WHERE s04_timestamp BETWEEN :startDate AND :endDate GROUP BY sorter, induct, DATE_FORMAT(s04_timestamp, "%Y-%m-%d %H:%i:00") ) SELECT time, /* Counts: */ total AS total_count, single_carrier AS single_carrier_count, double_carrier AS double_carrier_count, /* PPH: */ ROUND(total*60) AS total_pph, ROUND(single_carrier*60) AS single_carrier_pph, ROUND(double_carrier*60) AS double_carrier_pph, /* Percents: */ ROUND(total/(SUM(total) OVER(PARTITION BY induct, time)), 4) AS 'total/sorter_percent', ROUND(single_carrier/total, 4) AS single_carrier_percent, ROUND(double_carrier/total, 4) AS double_carrier_percent FROM INDUCTS WHERE induct = :induct;