Top 20% of Customers [message #224537] |
Wed, 14 March 2007 11:05 |
oraanu
Messages: 22 Registered: September 2005 Location: Boston
|
Junior Member |
|
|
Hi All,
I have a table 'customer' with Cust_name,Address,Phone_num, total_scripts columns.I need to find the top 20% of total customers order by total_scripts DESC.
Can anyone help me how to do this.
Thanks in Advance
Anu
|
|
|
|
|
Re: Top 20% of Customers [message #224688 is a reply to message #224641] |
Thu, 15 March 2007 02:51 |
|
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
I'd have a look at the PERCENT_RANK() analytic function:
SQL> select last_name
2 , salary
3 , percent_rank() OVER ( ORDER BY salary DESC
4 , commission_pct DESC
5 ) pct
6 from employees
7 where department_id > 80
8 /
LAST_NAME SALARY PCT
------------------------- ---------- ----------
King 24000 0
Kochhar 17000 .1
De Haan 17000 .1
Greenberg 12000 .3
Higgins 12000 .3
Faviet 9000 .5
Gietz 8300 .6
Chen 8200 .7
Urman 7800 .8
Sciarra 7700 .9
Popp 6900 1
11 rows selected.
SQL> select last_name
2 , salary
3 from ( select last_name
4 , salary
5 , percent_rank() OVER ( ORDER BY salary DESC
6 , commission_pct DESC
7 ) pct
8 from employees
9 where department_id > 80
10 )
11 where pct <= .2
12 /
LAST_NAME SALARY
------------------------- ----------
King 24000
Kochhar 17000
De Haan 17000
SQL>
MHE
|
|
|