SELECT c.name as `Category Name`,
p.name as `Product Name`,
COUNT(a.id) as `Items Sold`,
p.id as `Product ID` 
FROM hb_invoices `i` 
INNER JOIN hb_invoice_items `it` ON (it.invoice_id = i.id) 
INNER JOIN hb_accounts `a` ON (a.id = it.item_id 
AND it.type = 'Hosting') 
INNER JOIN hb_products `p` ON (a.product_id = p.id) 
INNER JOIN hb_categories `c` ON (c.id = p.category_id) 
WHERE
 i.status = 'Paid' 
AND i.datepaid > :date_bottom 
AND i.datepaid < :date_top 
AND i.id IN (SELECT invoice_id 
FROM hb_orders) 
GROUP BY p.id 

ORDER BY `Items Sold` DESC