暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

How do I report the top 10 product members of the sales team in each state/county ?

2011-01-01
813

The Oracle (tm) Users' Co-Operative FAQ

How do I report the top 10 product members of the sales team in each state/county ?


Author's name: Rajeev Garg

Author's Email: rajeev..garg@indiatimes.com

Date written: March 13, 2002

Oracle version(s): All

How do I report the top 10 product members of the sales team in each state/county ?

Back to index of questions


Suppose you table is something like this

Table Sales_Data
Column 1:  . State
Column 2:  . Member_id
Column 3:  . Criterion (Where criterion is something you use to define your top ten. (like no.of units sold etc.))

There are two ways to write this query. Using functions that have been made available in the 8i version or doing it without them. Of course, the pre 8i version is good for 8i too. But why slog when somebody has done the work for you already!

Pre 8i

select state, memberid, criterion
from (select rownum - (select count(*) from sales_data where state < a.state) as member_rank, criterion, state, memberid
                   from (select memberid, criterion, state
                             from sales_data
                             order by state,criterion desc ) a) b
where b.member_rank < 11;

OR Post 8i,

You can use the rank function

Select a.state, a.member_id,a.criterion
          from (select member_id, state ,criterion,rank() over(partition by state order by criterion desc) as member_rank
                     from sales_data ) a
where a.member_rank < 11
order by a.state, a.member_rank


Further Reading: n/a



最后修改时间:2020-04-16 15:13:10
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论