I have two matters I need help with in using superset.
the client wants to see the top 10 products from table Sales (ie: product count >300), and the rest of them displayed as "Others" (in a pie chart). How do I get the other products to be grouped into "Others", dynamically?
If they filter by one of the products (which would fall under "Others" category), they want to be able to see the data. Any ideas?
Many thanks in advance.
I tried the following query but it is not doing what I expected:
SELECT
CASE
WHEN t.product IN (SELECT product FROM Sales GROUP BY product HAVING COUNT(*) >= 30) THEN t.product
ELSE 'Others'
END AS grouped_product,
COUNT(*) AS product_count
FROM
Sales AS t
GROUP BY
grouped_product;
Output: Selection of top 10 ranking products without hardcoding 300 and the rest would be in "Others". Using Maria-DB
You can perform aggregation to obtain the count of products and then use
ROW_NUMBER()(orDENSE_RANK()in cases where values are tied) to assign a sequential ranking number within each partition :And by employing integer division using
DIV, we can pinpoint the first n products ( replace 3 with any desired number ) :Which for this dataset :
Will results :
Demo here