SELECT p.name,SUM(tsl.quantity) product_sales,(SUM(tsl.quantity)*100.0/SUM(SUM(tsl.quantity)) OVER (PARTITION BY p.category_id)) market_share FROM products p 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 p.id,p.name,p.category_id ORDER BY market_share DESC;