To create a Top 3 Bestsellers per Category widget, process daily sales data by first grouping it by category and summing the total units sold for each product. The rank function addresses ties, allowing products with the same sales count to share identical rankings without skipping numbers for subsequent products. After sorting the data by units sold in descending order, extract the top three products from each category. The final step involves aggregating these results to ensure a complete display of the bestsellers on the homepage.
To identify the top-selling products in each category, begin by grouping the sales data by category and summing the total units sold for each product in that category.
Handle ties in sales by using the rank function. Products with the same units sold will share the same rank without skipping numbers for subsequent ranks.
Sort the grouped data by total units sold in descending order to facilitate the top 3 selection process from each category.
Finally, use the collect_list function to aggregate the product details for the top 3 products per category, ensuring that data integrity is maintained.
Collection
[
|
...
]