Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3938794
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:13:31+00:00 2026-05-20T00:13:31+00:00

I have a query which is something like this SELECT t.category, tc.product, tc.sub-product, count(*)

  • 0

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.value
 GROUP BY t.category, tc.product, tc.sub-product;

Now in my query I want to get top 10 products for every category (top by sales ) and for every category I need top 5 sub category (top by sales)

You can assume the problem statement as something like this :

Get top 10 products for each category by sales and for each product get top 5 sub-products by sales .

  • Here category can be Books
  • Product can be Harry Porter book
  • sub productcan be HarryPorter series 5

Sample input data format

category |product |subproduct |Sales [count (*)]

abc   test1    test11     120

abc   test1    test11     100

abc   test1    test11     10

abc   test1    test11     10

abc   test1    test11     10

abc   test1    test11     10

abc   test1    test12     10

abc   test1    test13     8

abc   test1    test14     6

abc   test1    test15     5

abc   test2    test21     80

abc   test2    test22     60

abc   test3    test31     50

abc   test3    test32     40

abc   test4    test41     30

abc   test4    test42     20

abc   test5    test51     10

abc   test5    test52     5 

abc   test6    test61     5 

|

|

|

bcd   test2    test22     10 

xyz   test3    test31     5 

xyz   test3    test32     3 

xyz   test4    test41     2

Output would be “

top 5 rf for (abc) -> abc,test1(289) abc,test2 (140), abc test3 (90), abc test4(50) , abc test5 (15)

top 5 rfm for (abc,test1) -> test11(260),test12(10),test13(8),test14(6),test15(5) and so on

My query is failing because results are really huge . I am reading about oracle analytic functions like rank. Can someone help me modifying this query using analytical functions. Any other approach can also work.

I am referring to this http://www.orafaq.com/node/55. But unable to get a right sql query for this.

Any help would be appreciated..I am like stuck for 2 days on this 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:13:32+00:00Added an answer on May 20, 2026 at 12:13 am

    There are probably reasons not to use analytical functions, but using analytical functions alone:

    select am, rf, rfm, rownum_rf2, rownum_rfm
    from
    (
        -- the 3nd level takes the subproduct ranks, and for each equally ranked
        -- subproduct, it produces the product ranking
        select am, rf, rfm, rownum_rfm,
          row_number() over (partition by rownum_rfm order by rownum_rf) rownum_rf2
        from
        (
            -- the 2nd level ranks (without ties) the products within
            -- categories, and subproducts within products simultaneosly
            select am, rf, rfm,
              row_number() over (partition by am order by count_rf desc) rownum_rf,
              row_number() over (partition by am, rf order by count_rfm desc) rownum_rfm
            from
            (
                -- inner most query counts the records by subproduct
                -- using regular group-by. at the same time, it uses
                -- the analytical sum() over to get the counts by product
                select tg.am, ttc.rf, ttc.rfm,
                  count(*) count_rfm,
                  sum(count(*)) over (partition by tg.am, ttc.rf) count_rf
                from tg inner join ttc on tg.value = ttc.value
                group by tg.am, ttc.rf, ttc.rfm
            ) X
        ) Y
        -- at level 3, we drop all but the top 5 subproducts per product
        where rownum_rfm <= 5   -- top  5 subproducts
    ) Z
    -- the filter on the final query retains only the top 10 products
    where rownum_rf2 <= 10  -- top 10 products
    order by am, rownum_rf2, rownum_rfm;
    

    I used rownum instead of rank so you don’t ever get ties, or in other words, ties will be randomly decided. This also doesn’t work if the data is not dense enough (less than 5 subproducts in any of the top 10 products – it may show subproducts from some other products instead). But if the data is dense (large established database), the query should work fine.


    The below makes two passes of the data, but returns correct results in each case. Again, this is a rank-without-ties query.

    select am, rf, rfm, count_rf, count_rfm, rownum_rf, rownum_rfm
    from
    (
        -- next join the top 10 products to the data again to get
        -- the subproduct counts
        select tg.am, tg.rf, ttc.rfm, tg.count_rf, tg.rownum_rf, count(*) count_rfm,
            ROW_NUMBER() over (partition by tg.am, tg.rf order by 1 desc) rownum_rfm
        from (
            -- first rank all the products
            select tg.am, tg.value, ttc.rf, count(*) count_rf,
                ROW_NUMBER() over (order by 1 desc) rownum_rf
            from tg
            inner join ttc on tg.value = ttc.value
            group by tg.am, tg.value, ttc.rf
            order by count_rf desc
            ) tg
        inner join ttc on tg.value = ttc.value and tg.rf = ttc.rf
        -- filter the inner query for the top 10 products only
        where rownum_rf <= 10
        group by tg.am, tg.rf, ttc.rfm, tg.count_rf, tg.rownum_rf
    ) X
    -- filter where the subproduct rank is in top 5
    where rownum_rfm <= 5
    order by am, rownum_rf, rownum_rfm;
    

    columns:

    count_rf : count of sales by product
    count_rfm : count of sales by subproduct
    rownum_rf : product rank within category (rownumber - without ties)
    rownum_rfm : subproduct rank within product (without ties)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query which looks like this: SELECT LossCost, CoverageID FROM BGILossCost] WHERE
I have a query which contains a subquery which is something like this IFNULL((SELECT
I have a query something like this: SELECT title, desc, date FROM tablename ORDER
I have a query that looks like this: SELECT OrganizationName, OrganizationID, ReceivableStatus, InvoiceFee FROM
I have a query like this: select a,b,c from table Now I have another
I have this query SELECT semester, COUNT(id) AS total, RIGHT(RTRIM(semester), 4) AS year FROM
I have a query that goes something like this: SELECT t1, t2, IF(MATCH(t2) AGAINST
I have this query which groups the results by ORDER#. SELECT ORDER#, MAX(SHIPDATE -
I have a query which produces a list of orders like this: Invoice Description
I have a Category class which looks like this: @Entity @Table(name = categories) public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.