oracle - SQL -- Get items responsible for top 50% of sales -
i have table this:
item_sales item_name sales item_name_1 5000 ... item_name_x 3
what want get items represent top 50% of sales. example, if total sales 10,000, item_name_1 alone represent 50% of sales.
i can total sales with:
select sum(sales) item_sales.
...and divide 2 how many sales 50% of sales is.
however, don't know how i'd go there getting top items represent 50% of sales.
you can using analytic functions:
select s.* (select item_name, sum(sales) sumsales, sum(sum(sales)) on (order sum(sales) desc) cumesales, sum(sum(sales)) on () totsales, item_sales group item_name ) s (cumesales - sumsales) < 0.5 * totsales;
the subquery calculates sales each item, 2 other values:
- the cumulative sales, highest item.
- the total sales.
the where
clause gets items , include 1 passes 50% threshold.
Comments
Post a Comment