SELECT pr.rack,pr.row,COUNT(t.id) sales,SUM(tsl.quantity) units_sold FROM product_racks pr JOIN products p ON pr.product_id=p.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 pr.rack,pr.row ORDER BY units_sold DESC;