WITH INDUCTS AS (
	(
		SELECT
			DATE_FORMAT(s04_timestamp, "%Y-%m-%d %H:%i:00") AS time,
			sorter,
			induct,
			COUNT(*) AS total
		FROM package_history
		WHERE s04_timestamp BETWEEN :startDate AND :endDate
		GROUP BY sorter, induct, time
	) UNION ALL (
		SELECT
			DATE_FORMAT(timestamp, "%Y-%m-%d %H:%i:00") AS time,
			"AR" AS sorter,
			lane_id AS induct,
			SUM(count) AS total
		FROM pe_history
		WHERE timestamp BETWEEN :startDate AND :endDate
		GROUP BY sorter, induct, time
   	)
)
SELECT
	time,
	/* Counts: */
	SUM(total) AS total_count,
	/* PPH: */
	ROUND(SUM(total)*60) AS total_pph,
	/* Percents: */
	ROUND(SUM(total)/(SUM(total) OVER(PARTITION BY sorter, time)), 4) AS 'total/sorter_percent'
FROM INDUCTS
WHERE induct = :induct
GROUP BY induct, time;