Expected Using group by and case I was able to get the result of each product statistics for one interval. But I want the result for each interval horiziontally (above image), noting that using the following query we can get the result vertically:
SELECT
p.category,
(CASE
WHEN TIMESTAMPDIFF(YEAR, p.dt, CURDATE()) BETWEEN 1 AND 5 THEN '1-5'
WHEN TIMESTAMPDIFF(YEAR, p.dt, CURDATE()) BETWEEN 6 AND 9 THEN '6-9'
WHEN TIMESTAMPDIFF(YEAR, p.dt, CURDATE()) BETWEEN 10 AND 15 THEN '10-15'
ELSE '16-20'
END) as Interval,
COUNT(*) AS totalNbr
FROM product p
GROUP BY p.category, Interval
I have tried using union but it is not working yet.