Top N analysis..
.Don't plz send the links ... plz provide with sme solution ...
....Suppose ..i want to find TOP 3 earners of the Company so tht i can do .by using ..Inline vies.......
...My query is ..how to find ...the THIRD highest Earner of the Company .......
The Table is Employees ..Which is located in HR schema.....
I don't understand what you are trying to say, sorry.
My point is:
If you know how to get the top 3 earners:
SQL> get t1
1 select v.*, rownum rn
2 from ( select *
3 from emp
4 order by sal desc
5 ) v
6* where rownum <= 3
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO RN
7839 KING PRESIDENT 17-NOV-81 5000 10 1
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 2
7902 FORD ANALYST 7566 03-DEC-81 3000 20 3
SQL> then it is easy to select only the 3rd row from this result set:
SQL> get t2
1 select *
2 from (
3 select v.*, rownum rn
4 from ( select *
5 from emp
6 order by sal desc
7 ) v
8 where rownum <= 3
9 ) v2
10* where v2.rn >= 3
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO RN
7902 FORD ANALYST 7566 03-DEC-81 3000 20 3
SQL> So I can not get your point.
Message was edited by:
Leo Mannhart
Similar Messages
-
Top n Analysis using correlated subquery
Please explain this query. It is doing top n analysis using correlated subquery. I need explaination of execution of this query.
Select distinct a.sal
From emp a
where 1=(select count ( distinct b.sal) from emp b
where a.sal <=b.sal)
Thanks in advanceTry breaking the query down and rewriting it in order to follow the logic;
SQL> --
SQL> -- Start by getting each salary from emp along with a count of all salaries in emp
SQL> --
SQL> select a.sal,
(select count (distinct b.sal) from scott.emp b ) count_sal
from scott.emp a
order by 1 desc
SAL COUNT_SAL
5000 12
3000 12
3000 12
2975 12
2850 12
2450 12
1600 12
1500 12
1300 12
1250 12
1250 12
1100 12
950 12
800 12
14 rows selected.
SQL> --
SQL> --Add a condition to the count for only salaries below or equal to the current salarySQL> --
SQL> select a.sal,
(select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) rank_sal
from scott.emp a
order by 1 desc
SAL RANK_SAL
5000 1
3000 2
3000 2
2975 3
2850 4
2450 5
1600 6
1500 7
1300 8
1250 9
1250 9
1100 10
950 11
800 12
14 rows selected.
SQL> --
SQL> -- Add a condition to only pick the nth highest salary
SQL> --
SQL> select a.sal,
(select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) rank_sal
from scott.emp a
where (select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) = 4
SAL RANK_SAL
2850 4
1 row selected.Hope this helps. -
Top n analysis using hierarchial queries
hi all,
can we do top n analysis in hierarchial queries using level pseudo columns. if so please give an example.
thanks and regards,
sri ram.Hi,
Analytic functions (such as RANK) often interfere with CONNECT BY queries. Do one of them in a sub-query, and the other in a super-query, as shown below.
If you do the CONNECT BY first, use ROWNUM (which is assigned after ORDER SIBLINGS BY is applied) to preserve the order of the CONNECT BY query.
WITH connect_by_results AS
SELECT LPAD ( ' '
, 3 * (LEVEL - 1)
) || ename AS iname
, sal
, ROWNUM AS r_num
FROM scott.emp
START WITH mgr IS NULL
CONNECT BY mgr = PRIOR empno
ORDER SIBLINGS BY ename
SELECT iname
, sal
, RANK () OVER (ORDER BY sal DESC) AS sal_rank
FROM connect_by_results
ORDER BY r_num
;Output:
INAME SAL SAL_RANK
KING 5000 1
BLAKE 2850 5
ALLEN 1600 7
JAMES 950 13
MARTIN 1250 10
TURNER 1500 8
WARD 1250 10
CLARK 2450 6
MILLER 1300 9
JONES 2975 4
FORD 3000 2
SMITH 800 14
SCOTT 3000 2
ADAMS 1100 12
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data. If you use only commonly available tables (such as those in the scott or hr schemas), then you don't have to post any sample data; just post the results.
Explain how you get those results from that data.
Always say what version of oracle you're using. -
How to use top-n analysis in oracle 8i?
I mean,take a example.
I am maintaining a database of a 1000 employees.I want to display the names of the employees who are getting top 10 salaries(more further top 100 salaries) using a SQL query in oracle 8i only.Please answer my problem.Sorry, my suggestion will return 10 emp with highest salaries, not all employees with 10 highest salaries. To get all employees with 10 highest salaries in 8i:
SQL> SELECT ename,
2 sal
3 FROM emp
4 WHERE sal IN (
5 SELECT sal
6 FROM (
7 SELECT sal
8 FROM emp
9 GROUP BY sal
10 ORDER BY sal DESC
11 )
12 WHERE rownum <= 10
13 )
14 /
ENAME SAL
KING 5000
FORD 3000
SCOTT 3000
JONES 2975
BLAKE 2850
CLARK 2450
ALLEN 1600
TURNER 1500
MILLER 1300
MARTIN 1250
WARD 1250
ENAME SAL
ADAMS 1100
12 rows selected.
SQL> SY. -
Performing Top-n Analysis (but per group)
The Oracle University Guide SQL Volume 2 says:
To perform Top-n Analysis the general syntax is
SELECT [column_list], ROWNUM
FROM (SELECT [column_list]
FROM table
ORDER BY Top-N_column)
WHERE ROWNUM <= N;
for example
To display the top three earner names and salaries
from the EMPLOYEES table.
SELECT ROWNUM as RANK, last_name, salary
FROM (SELECT last_name,salary FROM employees
ORDER BY salary DESC)
WHERE ROWNUM <= 3;
or to display the four most senior employees in the company.
SELECT ROWNUM as SENIOR,E.last_name, E.hire_date
FROM (SELECT last_name,hire_date FROM employees
ORDER BY hire_date)E
WHERE rownum <= 4;
but what about if I have groups? for example if I want to display the 3 top earners per department?
In my case now
I want to fetch the top 4 items per category, with the biggest quantity (posothta)
SELECT ROWNUM as RANK,
H.KATHG_EIDOYS,
H.KATHG_EIDOYS_DESCR,
H.EIDOS,
H.EIDOS_DESCR,
H.CODE_SUP_BASIKOS_NAME,
H.RAFI_CODE,
H.LINES,
H.POSOTHTA
from (
SELECT B.KATHG_EIDOYS,
D.DESCRIPTION KATHG_EIDOYS_DESCR,
B.CODE EIDOS,
B.DESCRIPTION EIDOS_DESCR,
S.NAME CODE_SUP_BASIKOS_NAME,
C.RAFI_CODE,
COUNT(A.FLD_SEQ_NUM) LINES,
nvl(SUM(decode(k.INV_APOGRAFH_FLAG,'0', decode(k.INV_EXAGOGH_POSOTHTA,1, a.POSOTHTA_TIMOLOGHSHS,2,-a.POSOTHTA_TIMOLOGHSHS))),0) POSOTHTA
FROM ERP_EIDOI_ANA_RAFI C,
ERP_KODIKOI_KINHSHS K,
ERP_POLHSEIS_DETAILS A,
ERP_SUP_CUST S,
ERP_KATHG_EIDON D,
ERP_EIDH b
WHERE B.COMPANY = DECODE(1,1,'9',B.COMPANY)
AND a.COMPANY_KK=K.COMPANY
AND a.KK_CODE=K.CODE
and A.company_WAREHOUSE = c.COMPANY_WARE(+)
and A.MASTER_WAREHOUSE = c.MASTER_WARE_CODE(+)
and A.CODE_WAREHOUSE = c.DETAIL_WARE_CODE(+)
and A.COMPANY_EIDOS = c.COMPANY_EIDOS(+)
and A.EIDOS = c.CODE_EIDOS (+)
AND C.DEFAULT_FLAG (+)= 1
AND b.code = a.EIDOS
and b.company = a.COMPANY_EIDOS
AND D.CODE= B.KATHG_EIDOYS
AND D.COMPANY= B.COMPANY_KATHG_EIDOYS
AND B.COMPANY_SUP_BASIKOS = S.COMPANY
AND B.CODE_SUP_BASIKOS = S.CODE
AND B.PROMHTHEYTHS_FLAG_BASIKOS = S.PELATHS_PROMHTHEYTHS_FLAG
AND /*&p_where*/
a.COMPANY='9' and (a.group_source) = '10' and (A.MASTER_WAREHOUSE) = '01' and (A.CODE_WAREHOUSE) = '0101' and (a.hmerom_parast) >= to_date('01/01/2006','dd/mm/rrrr') and (a.hmerom_parast) <= to_date('25/05/2006','dd/mm/rrrr')
GROUP BY B.KATHG_EIDOYS, D.DESCRIPTION, B.CODE, B.DESCRIPTION, S.NAME,C.RAFI_CODE
ORDER BY 8 DESC
) H
where 1=1 and ROWNUM <= 4
this select does not bring me the desired results, because if for example
category 01 has 10 items
and category 02 has 2 items,
this select will bring me only the first four rows
and not the items from the 02 category.
If you understand what is the case I will wait for your replies.
Thanks in advanceHi,
Here is an example. It gives you customers ids with highest salary per department.
SELECT CUSTOMER_ID, SALARY, RANK, DEPARTMENT
FROM (SELECT CUSTOMER_ID, SALARY, DEPARTMENT,
RANK() OVER (PARTITION BY DEPARTMENT ORDER BY SALARY DESC) AS RANK
FROM TABLEA)
WHERE RANK < 2;
Peter D. -
What is Top-N analysis.?? can some one explain. I encountered this while going thru
the Sql book
The ORDER BY clause in the subquery is not
needed unless you are performing Top-N analysis.That means you are doing something like this in order to get top five paid people::
SELECT * FROM
( SELECT empno, ename, sal+nvl(comm,0) as remuneration
FROM emp
ORDER BY remuneration DESC )
WHERE rownum <= 5
/We have to do the ORDER BY in a sub-query because rownum gets applied before the rows are sorted.
Cheers, APC -
An Oracle University Material in Sql Says
The high-level structure of a Top-N analysis query is:
SELECT [column_list], ROWNUM
FROM (SELECT [column_list]
FROM table
ORDER BY Top-N_column)
WHERE ROWNUM <= N;
For example to display the top three earner names and salaries from the EMPLOYEES table:
SELECT ROWNUM as RANK, last_name, salary
FROM (SELECT last_name,salary FROM employees
ORDER BY salary DESC)
WHERE ROWNUM <= 3;
My question is
If, instead of this query, I write
1)
SELECT ROWNUM as RANK, last_name, salary
FROM employees
WHERE ROWNUM <= 3
ORDER BY salary DESC
or
2)
SELECT ROWNUM as RANK, last_name, salary
FROM ( SELECT last_name,salary
FROM employees
WHERE ROWNUM <= 3
ORDER BY salary DESC
is any difference?
The results in schema hr are the same.............
Thank youis any difference? yes, there is!
SQL> with t as (select 1 num from dual union all
2 select 4 from dual union all
3 select 3 from dual union all
4 select 2 from dual)
5 --
6 SELECT ROWNUM as RANK, num
7 FROM (SELECT num FROM t ORDER BY num desc)
8 WHERE ROWNUM <= 3;
RANK NUM
1 4
2 3
3 2
SQL>
SQL> with t as (select 1 num from dual union all
2 select 4 from dual union all
3 select 3 from dual union all
4 select 2 from dual)
5 --
6 SELECT ROWNUM as RANK, num
7 FROM t
8 WHERE ROWNUM <= 3
9 ORDER BY num DESC
10 /
RANK NUM
2 4
3 3
1 1
SQL>
SQL> with t as (select 1 num from dual union all
2 select 4 from dual union all
3 select 3 from dual union all
4 select 2 from dual)
5 --
6 SELECT ROWNUM as RANK, num
7 FROM (SELECT num FROM t
8 WHERE ROWNUM <= 3
9 ORDER BY num DESC)
10 /
RANK NUM
1 4
2 3
3 1
SQL> rownum is confered before ordering is made.
Thats why you should place the subquery with ordering in the inline view. -
TOP N analysis with same values
Dear Members,
Suppose we have the following data in the table Student.
Sname GPA
Jack 4.0
Smith 3.7
Rose 3.5
Rachel 3.5
Ram 2.8
I have seen many questions in this forum which gives good queries for TOP N analysis. But in my case those are not working.
There are total 5 students. I should write a query which should take an input and should give the students with top gpa as output in desc order.
Suppose if i give 4 as input i must get 4,3.7,3.5,3.5,2.8Gpa's since we have 2 gpa's which are same. Suppose i give 3 as the input i must get 4,3.7,3.5 and 3.5 GPA's.
The query must consider the GPA's which are same as one not different. How can we achive this. i.e the top three students (suppose input is 3) must be
Jack 4.0
Smith 3.7
Rose 3.5
Rachel 3.5
It must also include Rachel.
Any help is greatly appreciated.
Thanks
SandeepSQL> select * from test;
NAME GPA
Jack 4
Smith 3.7
Rose 3.5
Rachel 3.5
Ram 2.8
SQL> select name,gpa
2 from
3 (select name,gpa,dense_rank() over(order by gpa desc) rn
4 from test)
5 where rn <= 3
6 order by rn;
NAME GPA
Jack 4
Smith 3.7
Rose 3.5
Rachel 3.5 -
Top N Analysis with multiple columns
Hi
I am using Oracle 9i. I do have a table which contains datewise promotional material types for an organisation.
The structure is as follows:
CREATE TABLE TEST
(CDATE DATE,
BROCHURE VARCHAR2(1),
WEBSITE VARCHAR2(1),
DIRECT_MAIL VARCHAR2(1),
PRESS_RELEASE VARCHAR2(1),
JOURNAL_AD VARCHAR2(1)
and the sample data is as follows:
CDate Brochure Website Direct_Mail Press_Release Journal_Ad
01/04/1996 Y Y Y N N
02/04/1996 Y Y N N N
23/06/1996 Y N Y Y N
13/09/1996 Y Y N N N
01/04/1997 Y Y N N N
02/04/1997 Y Y Y N Y
23/06/1997 N Y N N Y
13/09/1997 Y Y N N N
01/04/1998 Y Y Y N N
02/04/1998 Y N N Y N
23/06/1998 N Y N N Y
13/09/1998 Y Y N N Y
01/04/1999 Y Y Y N Y
02/04/1999 Y N N Y N
23/06/1999 N Y N N N
13/09/1999 Y Y Y N N
I want to have year wise top 4 promotional types in terms of count of 'Y' only. The result should be like as follows:
YEAR:1996
TYPE COUNT
BROCHURE 4
WEBSITE 3
DIRECT_MAIL 2
PRESS_RELEASE 1
JOURNAL_AD 0
YEAR:1997
TYPE COUNT
WEBSITE 4
BROCHURE 3
JOURNAL_AD 2
DIRECT_MAIL 1
PRESS_RELEASE 1
Please suggest a solution for the same. I am not able to sort it for multiple columns.
Regards
MSOne of the questions that must be asked when you have a requirement to only show the top N ranked items in a list, is "what about a tie in the ranking?".
Oracle has two ranking functions that allow you to deal with either requirement - RANK and DENSE_RANK. Both operate as either analytic or aggregate functions, so either will work for your requirements. The previous posting by Miguel demonstrated how to decode your Y/N flags and pivot the data.
In this example, I've taken the liberty of adding some data to year 2000 that will show the difference between RANK and DENSE_RANK as well as how to use them to filter your results.
First, here's the decoded/pivoted data:
SQL>WITH test AS
2 ( SELECT TO_DATE('01/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
3 UNION ALL SELECT TO_DATE('02/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
4 UNION ALL SELECT TO_DATE('23/06/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
5 UNION ALL SELECT TO_DATE('13/09/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
6 UNION ALL SELECT TO_DATE('01/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
7 UNION ALL SELECT TO_DATE('02/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
8 UNION ALL SELECT TO_DATE('23/06/1997','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
9 UNION ALL SELECT TO_DATE('13/09/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
10 UNION ALL SELECT TO_DATE('01/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
11 UNION ALL SELECT TO_DATE('02/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
12 UNION ALL SELECT TO_DATE('23/06/1998','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
13 UNION ALL SELECT TO_DATE('13/09/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
14 UNION ALL SELECT TO_DATE('01/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
15 UNION ALL SELECT TO_DATE('02/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
16 UNION ALL SELECT TO_DATE('23/06/1999','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
17 UNION ALL SELECT TO_DATE('13/09/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
18 UNION ALL SELECT TO_DATE('01/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
19 UNION ALL SELECT TO_DATE('02/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
20 UNION ALL SELECT TO_DATE('23/06/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
21 UNION ALL SELECT TO_DATE('13/09/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
22 )
23 SELECT cyear
24 ,ctype
25 ,RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) ranking
26 ,DENSE_RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) dense_ranking
27 FROM (SELECT TRUNC(CDATE,'Y') CYEAR
28 ,'BROCHURE' CTYPE, SUM(DECODE(BROCHURE, 'Y', 1, 0)) NUM_MEDIA
29 FROM test
30 GROUP BY TRUNC(CDATE,'Y')
31 UNION ALL
32 SELECT TRUNC(CDATE,'Y') CYEAR
33 ,'WEBSITE' CTYPE, SUM(DECODE(WEBSITE, 'Y', 1, 0))
34 FROM test
35 GROUP BY TRUNC(CDATE,'Y')
36 UNION ALL
37 SELECT TRUNC(CDATE,'Y') CYEAR
38 ,'DIRECT_MAIL' CTYPE, SUM(DECODE(DIRECT_MAIL, 'Y', 1, 0))
39 FROM test
40 GROUP BY TRUNC(CDATE,'Y')
41 UNION ALL
42 SELECT TRUNC(CDATE,'Y') CYEAR
43 ,'PRESS_RELEASE' CTYPE, SUM(DECODE(PRESS_RELEASE, 'Y', 1, 0))
44 FROM test
45 GROUP BY TRUNC(CDATE,'Y')
46 UNION ALL
47 SELECT TRUNC(CDATE,'Y') CYEAR
48 ,'JOURNAL_AD' CTYPE, SUM(DECODE(JOURNAL_AD, 'Y', 1, 0))
49 FROM test
50 GROUP BY TRUNC(CDATE,'Y')
51 )
52* order by cyear desc, ranking
53 /
CYEAR CTYPE RANKING DENSE_RANKING
01-Jan-2000 00:00:00 BROCHURE 1 1
01-Jan-2000 00:00:00 WEBSITE 1 1
01-Jan-2000 00:00:00 DIRECT_MAIL 3 2
01-Jan-2000 00:00:00 PRESS_RELEASE 4 3
01-Jan-2000 00:00:00 JOURNAL_AD 5 4
01-Jan-1999 00:00:00 BROCHURE 1 1
01-Jan-1999 00:00:00 WEBSITE 1 1
01-Jan-1999 00:00:00 DIRECT_MAIL 3 2
01-Jan-1999 00:00:00 PRESS_RELEASE 4 3
01-Jan-1999 00:00:00 JOURNAL_AD 4 3
01-Jan-1998 00:00:00 BROCHURE 1 1
01-Jan-1998 00:00:00 WEBSITE 1 1
01-Jan-1998 00:00:00 JOURNAL_AD 3 2
01-Jan-1998 00:00:00 DIRECT_MAIL 4 3
01-Jan-1998 00:00:00 PRESS_RELEASE 4 3
01-Jan-1997 00:00:00 WEBSITE 1 1
01-Jan-1997 00:00:00 BROCHURE 2 2
01-Jan-1997 00:00:00 JOURNAL_AD 3 3
01-Jan-1997 00:00:00 DIRECT_MAIL 4 4
01-Jan-1997 00:00:00 PRESS_RELEASE 5 5
01-Jan-1996 00:00:00 BROCHURE 1 1
01-Jan-1996 00:00:00 WEBSITE 2 2
01-Jan-1996 00:00:00 DIRECT_MAIL 3 3
01-Jan-1996 00:00:00 PRESS_RELEASE 4 4
01-Jan-1996 00:00:00 JOURNAL_AD 5 5You can see that in year 2000 there is a tie for first place (ranking #1). The RANK function will name the second highest count 3 (skipping the rank of 2 due to the tie), while the DENSE_RANK function will not skip a ranking.
Now, to filter on the ranking, wrap your query in another in-line view like this - but use which ever ranking function YOUR requirements call for:
SQL>WITH test AS
2 ( SELECT TO_DATE('01/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
3 UNION ALL SELECT TO_DATE('02/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
4 UNION ALL SELECT TO_DATE('23/06/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
5 UNION ALL SELECT TO_DATE('13/09/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
6 UNION ALL SELECT TO_DATE('01/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
7 UNION ALL SELECT TO_DATE('02/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
8 UNION ALL SELECT TO_DATE('23/06/1997','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
9 UNION ALL SELECT TO_DATE('13/09/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
10 UNION ALL SELECT TO_DATE('01/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
11 UNION ALL SELECT TO_DATE('02/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
12 UNION ALL SELECT TO_DATE('23/06/1998','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
13 UNION ALL SELECT TO_DATE('13/09/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
14 UNION ALL SELECT TO_DATE('01/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
15 UNION ALL SELECT TO_DATE('02/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
16 UNION ALL SELECT TO_DATE('23/06/1999','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
17 UNION ALL SELECT TO_DATE('13/09/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
18 UNION ALL SELECT TO_DATE('01/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
19 UNION ALL SELECT TO_DATE('02/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
20 UNION ALL SELECT TO_DATE('23/06/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
21 UNION ALL SELECT TO_DATE('13/09/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
22 )
23 SELECT * FROM (
24 SELECT cyear
25 ,ctype
26 ,RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) ranking
27 ,DENSE_RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) dense_ranking
28 FROM (SELECT TRUNC(CDATE,'Y') CYEAR
29 ,'BROCHURE' CTYPE, SUM(DECODE(BROCHURE, 'Y', 1, 0)) NUM_MEDIA
30 FROM test
31 GROUP BY TRUNC(CDATE,'Y')
32 UNION ALL
33 SELECT TRUNC(CDATE,'Y') CYEAR
34 ,'WEBSITE' CTYPE, SUM(DECODE(WEBSITE, 'Y', 1, 0))
35 FROM test
36 GROUP BY TRUNC(CDATE,'Y')
37 UNION ALL
38 SELECT TRUNC(CDATE,'Y') CYEAR
39 ,'DIRECT_MAIL' CTYPE, SUM(DECODE(DIRECT_MAIL, 'Y', 1, 0))
40 FROM test
41 GROUP BY TRUNC(CDATE,'Y')
42 UNION ALL
43 SELECT TRUNC(CDATE,'Y') CYEAR
44 ,'PRESS_RELEASE' CTYPE, SUM(DECODE(PRESS_RELEASE, 'Y', 1, 0))
45 FROM test
46 GROUP BY TRUNC(CDATE,'Y')
47 UNION ALL
48 SELECT TRUNC(CDATE,'Y') CYEAR
49 ,'JOURNAL_AD' CTYPE, SUM(DECODE(JOURNAL_AD, 'Y', 1, 0))
50 FROM test
51 GROUP BY TRUNC(CDATE,'Y')
52 )
53 )
54 where RANKING <= 4
55* order by cyear desc, ranking
56 /
CYEAR CTYPE RANKING DENSE_RANKING
01-Jan-2000 00:00:00 WEBSITE 1 1
01-Jan-2000 00:00:00 BROCHURE 1 1
01-Jan-2000 00:00:00 DIRECT_MAIL 3 2
01-Jan-2000 00:00:00 PRESS_RELEASE 4 3
01-Jan-1999 00:00:00 BROCHURE 1 1
01-Jan-1999 00:00:00 WEBSITE 1 1
01-Jan-1999 00:00:00 DIRECT_MAIL 3 2
01-Jan-1999 00:00:00 JOURNAL_AD 4 3
01-Jan-1999 00:00:00 PRESS_RELEASE 4 3
01-Jan-1998 00:00:00 BROCHURE 1 1
01-Jan-1998 00:00:00 WEBSITE 1 1
01-Jan-1998 00:00:00 JOURNAL_AD 3 2
01-Jan-1998 00:00:00 PRESS_RELEASE 4 3
01-Jan-1998 00:00:00 DIRECT_MAIL 4 3
01-Jan-1997 00:00:00 WEBSITE 1 1
01-Jan-1997 00:00:00 BROCHURE 2 2
01-Jan-1997 00:00:00 JOURNAL_AD 3 3
01-Jan-1997 00:00:00 DIRECT_MAIL 4 4
01-Jan-1996 00:00:00 BROCHURE 1 1
01-Jan-1996 00:00:00 WEBSITE 2 2
01-Jan-1996 00:00:00 DIRECT_MAIL 3 3
01-Jan-1996 00:00:00 PRESS_RELEASE 4 4 -
How to use Aggregate Functions during Top N analysis?
Say i want to find top 5 highest salaries and their totals and average. In that case how to use aggregate functions. Please give me an example on this.
Regards,
Renu
Message was edited by:
user642387Hi,
Yes, you can do that with aggregate functions.
First, do a sub-query to retrieve all the salaries (in descending order), then say "WHERE ROWNUM <= 5" in the main query. Use the aggregate SUM and AVG functions in the main query.
Analytic functions are easier to use for jobs like this, once you get familiar with them. If you're not leaving the field this month, then it's probably worthwhile for you to get familiar with analytic functions. -
How to Calculate Top-n analysis
Hello,
I am using Cross Tab Report to get Customer and countriwise revenue report. Rows represents customer and columns represent company. I have used "Total" function to calculate Customer wise Total Revenue Amount. Thus total revenue amount gets printed at the end of each row.
I can use sort o Toal Revenue column in High to Low order to print highest paying customers. But my users want me to pass parameter where in they will mention number of top paying cusotmers to be printed in report. For eg: if parameter value is 20 then, report should print Top 20 Customers with highest revenue amount.
Please help.
Thanks,
AnishAdditionally, there are some analytic templates in Plus that may help you with your rank calculation.
Templates:
http://download.oracle.com/docs/html/B13915_04/calculations.htm#i1035520
Examples:
http://download.oracle.com/docs/html/B13915_04/dialog_reference.htm#i1026848
Regards,
Steve. -
Performance of top-n analysis queries
Hi,
I am trying to understand how top-n queries work w.r.t performance.
I have 10 lakh records in my table out of my query filters.
I just want to retrieve top 1000 records.
i.e.
select * from t where x=y; -- returns 10,00,00,000 records
To retrieve top 1000 records I use
select * from
(select * from t where x=y
order by x
) where rownum<1001;
How can I actually measure that the cost is reduced?
Or to put my question in other words, How can I prove that 2nd query is faster or better than 1st query (does it actually improves performance)?
Thanks.Kavipriya wrote:
Hi,
I am trying to understand how top-n queries work w.r.t performance.
I have 10 lakh records in my table out of my query filters.
I just want to retrieve top 1000 records.
i.e.
select * from t where x=y; -- returns 10,00,00,000 records
To retrieve top 1000 records I use
select * from
(select * from t where x=y
order by x
) where rownum<1001;
How can I actually measure that the cost is reduced?
Or to put my question in other words, How can I prove that 2nd query is faster or better than 1st query (does it actually improves performance)?
Thanks.You cannot compare your two query. They are entirely different. First one gets rows from table T for the condition X=Y. But the second query does more than that. It filters the table T for the condition X=Y then Orders the result set based on the column X and finally filters it for the first 1000 records.
So how would you compare two query that are entirely different? -
Top n analysis without analytical function or rownum
Hi
I am working on Oracle 9i and i have the following query.
My data is like as
Year Type Total
1996 A 23
1996 B 34
1996 C 19
1996 D 11
1996 E 45
1996 F 32
1997 A 12
1997 B 11
1997 C 34
1997 D 45
1997 E 67
1997 F 11
My requirement is to get the top 4 value year wise from the above data. All i need is without the use of ROWNUM as well as other analytical functions like RANK, PARTITION BY etc.
The required result set is
Year Type Total
1996 E 45
1996 B 34
1996 F 32
1996 A 23
1997 E 67
1997 D 45
1997 C 34
1997 A 12
Thanks
MSMaybe you can try this:
SQL> WITH TABLE_A AS
2 (
3 SELECT '1996' COL1, 'A' COL2, 23 COL3 FROM DUAL
4 UNION ALL
5 SELECT '1996' COL1, 'B' COL2, 34 COL3 FROM DUAL
6 UNION ALL
7 SELECT '1996' COL1, 'C' COL2, 19 COL3 FROM DUAL
8 UNION ALL
9 SELECT '1996' COL1, 'D' COL2, 11 COL3 FROM DUAL
10 UNION ALL
11 SELECT '1996' COL1, 'E' COL2, 45 COL3 FROM DUAL
12 UNION ALL
13 SELECT '1996' COL1, 'F' COL2, 32 COL3 FROM DUAL
14 UNION ALL
15 SELECT '1997' COL1, 'A' COL2, 12 COL3 FROM DUAL
16 UNION ALL
17 SELECT '1997' COL1, 'B' COL2, 11 COL3 FROM DUAL
18 UNION ALL
19 SELECT '1997' COL1, 'C' COL2, 34 COL3 FROM DUAL
20 UNION ALL
21 SELECT '1997' COL1, 'D' COL2, 45 COL3 FROM DUAL
22 UNION ALL
23 SELECT '1997' COL1, 'E' COL2, 67 COL3 FROM DUAL
24 UNION ALL
25 SELECT '1997' COL1, 'F' COL2, 11 COL3 FROM DUAL
26 )
27 SELECT COL1, COL2, COL3 FROM
28 (
29 SELECT
30 COL1,
31 COL2,
32 COL3,
33 (SELECT COUNT(1) FROM TABLE_A B WHERE A.COL1 = B.COL1 AND B.COL3 > A.COL3) ORDER_NUM
34 FROM
35 TABLE_A A
36 ORDER BY
37 COL1, COL3 DESC
38 ) WHERE ORDER_NUM < 4
39 /
COL1 C COL3
1996 E 45
1996 B 34
1996 F 32
1996 A 23
1997 E 67
1997 D 45
1997 C 34
1997 A 12 -
Top-N Analysis query in explicit cursor
Hi all,
I am using developer tool Reports 6i and database 10g
i have written a cursor query
cursor cur_voy is
select * from(select bvd_voy,bvd_vessel from bl_details,bl_voyage_details
where bdl_company =bvd_company
order by bvd_reference,bvd_serial asc)
where rownum<=1;
but its show error :- Encountered the symbol 'order' when expecting one of the following
.(),*,@,%,&,-,+,/,mod,rem ..........
why it shows errors ?
please help
Thanks in advance
RinzHi,
Please post the complete block/proc...
The sql seems ok..
similar logic posted below...
declare
cursor c1 is
WITH t AS
(SELECT 1 ID, 'AA' NAME
FROM DUAL
UNION ALL
SELECT 2 ID, 'BB' NAME
FROM DUAL
UNION ALL
SELECT 3 ID, 'CC' NAME
FROM DUAL),
t2 AS
(SELECT 1 ID, 'DD' name2
FROM DUAL
UNION ALL
SELECT 2 ID, 'EE' name2
FROM DUAL
UNION ALL
SELECT 3 ID, 'FF' name2
FROM DUAL)
SELECT *
FROM (SELECT NAME, name2
FROM t, t2
WHERE t.ID = t2.ID
ORDER BY t.ID, t2.ID asc)
where rownum<=1 ;
begin
null;
end; -
All about PnL[Profit and Loss] and SGA[Sales Growth analysis]
hello SDN.
I have just joined a consumer goods MNC as an executive trainee working on SAP BI.
My current profile is on SGA loading and PnL loading.
Can anyone tell me more about SGA and PnL.
I do not want the SAP technical points.
I need the core business explanation for these topics.
Why are they needed and what role they play at the management level, etc...
Waiting for the reply eagerly
ThanksHi,
I think u r asking more on functional analysis, Management uses Sales Growth Analysis (also called Top Line analysis) to know the growth rate in their Revenues, it shows their Market share and customer retention and new customers added, whether their promotional campaigns worked or not? like this it helps the management.
Coming to Profit and loss analysis or also called as Bottom line analysis, it shows their efficiency in optimum utilization of resources, increase or decrease in operating expenses etc.
based on the above two analysis, the scenarios may be like this:
Revenues increased, but profit come down, what r the reasons?
Revenues decreased, but profits increased, reasons?
etc.
this may be useful
Thanks
Ganesh
Maybe you are looking for
-
Msi x48c Platinum - HD audiodrivers
Hi ! I have downloadet the lastest drivers from MSI with HD audio drivers realtek but i keep getting an yellow Questionmark (?) in my hardware list, It wont update and find the drivers after few minute seach on my HDD and cd rom from msi. Anyone with
-
I have the following data flow: ADO Source Input Column: Cookies ID: 7371 Datatype: DT_NUMERIC Output Column: Cookies ID: 7391 Datatype: DT_NUMERIC DATA CONVERSION TASK Input Column: Cookies ID: 1311 Datatype: DT_NUMERIC
-
hi to all experts, i have a requirement of listing purchase orders created only on Sunday and Saturday,please help me out im struggling with is report from 2 days. any sample code will help
-
New Classic iPod will not restore and can't eject from iTunes.
Hi everybody. I just bought a new 160 GB Classic a week ago and promptly attached it to my Windows based laptop and installed the newest firmware. I can add shows and songs to it with no problems, but if I do, I can NOT then eject the iPod from iTune
-
Import/Export Portal Content between EP6 6.20 and 6.40
Hi there, We are about to setup a second portal in Europe on 6.40, while the US portal will still be a EP6 SP2 on 6.20 for some time. I was wondering about the possibilities to share portal content between those 2 installations, especially iViews and