To divide suppliers into 5 equal buckets based on their order contribution for the month, you can use a combination of SQL window functions and a subquery. Assuming you have a table called orders with columns supplier_id and order_contribution, you can use the following query:
sql
SELECT
supplier_id,
SUM(order_contribution) AS order_contribution,
ROW_NUMBER() OVER (ORDER BY SUM(order_contribution) DESC) AS row_number
FROM
orders
GROUP BY
supplier_id
ORDER BY
row_number;
In this query, the ROW_NUMBER() window function assigns a row number to each supplier in descending order based on their total order contribution. The ORDER BY clause is used to sort the results by the row number. Finally, the SELECT statement is used to display the supplier_id, the sum of their order contribution, and the row number assigned by the window function.
After running this query, you can use the row number to divide suppliers into 5 equal buckets. For example, if you want to include the top 20% of suppliers in the first bucket, you can filter the results like this:
sql
SELECT
supplier_id,
SUM(order_contribution) AS order_contribution
FROM
(
SELECT
supplier_id,
SUM(order_contribution) AS order_contribution,
ROW_NUMBER() OVER (ORDER BY SUM(order_contribution) DESC) AS row_number
FROM
orders
GROUP BY
supplier_id
) AS subquery
WHERE
row_number <= (SELECT MAX(row_number) * 0.2 FROM subquery)
GROUP BY
supplier_id;
In this query, the outer query selects the supplier_id and the sum of their order contribution and then filters the results based on the row number. The subquery calculates the maximum row number and then multiplies it by 0.2 to determine the threshold for the first bucket. This approach can be used to divide suppliers into any number of buckets based on their order contribution.