SELECT CASE WHEN HOUR(t.transaction_date)<12 THEN "Morning" WHEN HOUR(t.transaction_date)<17 THEN "Afternoon" ELSE "Evening" END day_part,c.name category,COUNT(t.id) transactions,SUM(tsl.quantity) units FROM transactions t JOIN transaction_sell_lines tsl ON t.id=tsl.transaction_id JOIN products p ON tsl.product_id=p.id JOIN categories c ON p.category_id=c.id WHERE t.type="sell" GROUP BY 1,c.id,c.name ORDER BY units DESC;