Get top 10 products for every category

后端 未结 2 788
失恋的感觉
失恋的感觉 2020-12-16 07:29

I have a query which is something like this

SELECT 
t.category, 
tc.product, 
tc.sub-product,
 count(*) as sales 
 FROM tg t, ttc tc
 WHERE t.value = tc.valu         


        
2条回答
  •  死守一世寂寞
    2020-12-16 07:52

    It's guesswork, but you could probably start from something like this:

    drop table category_sales;
    

    Some test data:

    create table category_sales (
       category    varchar2(14),
       product     varchar2(14),
       subproduct  varchar2(14),
       sales       number
    );
    
    begin
    
      for cate in 1 .. 10 loop
      for prod in 1 .. 20 loop
      for subp in 1 .. 30 loop
    
          insert into category_sales values (
                 'Cat '  || cate,
                 'Prod ' || cate||prod,
                 'Subp ' || cate||prod||subp,
                  trunc(dbms_random.value(1,30 + cate - prod + subp))
          );
    
      end loop; end loop; end loop;
    
    end;
    /
    

    The actual query:

    select * from (
      select 
        category,
        product,
        subproduct,
        sales,
        category_sales,
        product_sales,
        top_subproduct,
        -- Finding best products within category:
        dense_rank () over (
          partition by category
          order     by product_sales desc
        ) top_product 
      from (
        select 
          -- Finding the best Subproducts within
          -- category and product:
          dense_rank () over (
             partition by category, 
                          product 
             order     by sales desc
          )                             top_subproduct,
          -- Finding the sum(sales) within a 
          -- category and prodcut
          sum(sales) over (
             partition by category, 
                          product
          )                             product_sales,
          -- Finding the sum(sales) within 
          -- category
          sum(sales) over (
             partition by category
          )                             category_sales,
          category,
          product,
          subproduct,
          sales
        from
          category_sales
      )
    )
    where 
    --    Only best 10 Products
          top_product       <= 10 and
    --    Only best 5 subproducts:
          top_subproduct    <= 5
    -- "Best" categories first:
    order by 
          category_sales desc,
          top_product    desc,
          top_subproduct desc;
    

    In that query, the column category_sales returns the sum of sales of the category in whose record it is returned. That means, every record of the same category has the same category_sales. This column is needed to order the result set with the best (sales) categories first (order by ... category_sales desc).

    Similarly, product_sales is the sum of sales for a category-product combination. This column is used to find the best n (here:10) products in each category (where top_product <= 10).

    The column top_product is "created" with the dense_rank() over... analytical function. For the best product in a category,it's 1, for the second best it's 2 and so on (hence the where top_product <= 10.

    The columntop_suproduct is calculated in a similar fashion like top_product (that is with dense_rank).

提交回复
热议问题