SELECT c.name category,DATE(t.transaction_date) date,COUNT(t.id) sales FROM categories c JOIN products p ON c.id=p.category_id JOIN transaction_sell_lines tsl ON p.id=tsl.product_id JOIN transactions t ON tsl.transaction_id=t.id WHERE t.type="sell" GROUP BY c.id,c.name,DATE(t.transaction_date) ORDER BY date;