SELECT p.name,COUNT(DISTINCT t.id) sales_after_stockout FROM products p JOIN variation_location_details vld ON p.id=vld.product_id JOIN transaction_sell_lines tsl ON p.id=tsl.product_id JOIN transactions t ON tsl.transaction_id=t.id WHERE vld.qty_available=0 AND t.type="sell" GROUP BY p.id,p.name;