|
|
|
|
|
|
| Re: Top 2 Salary from Each Dept without using Analytic Function [message #642096 is a reply to message #642095] |
Wed, 02 September 2015 10:52   |
John Watson
Messages: 9003 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
come on, man. This doesn't work:orclz>
orclz> SELECT EMP_ID,EMP_NAME,SALARY,RNK FROM
2 (SELECT EMP_ID,EMP_NAME,SALARY,
3 RANK()OVER (PARTITION BY EMP_DEP_ID ORDER BY SALARY DESC) RNK
4 FROM EMP)
5 WHERE RNK IN (1,2);
RANK()OVER (PARTITION BY EMP_DEP_ID ORDER BY SALARY DESC) RNK
*
ERROR at line 3:
ORA-00904: "SALARY": invalid identifier
orclz> How is anyone supposed to know what you want with that? What tables are you using? And you haven't used [code] tags. Please do so next time.
|
|
|
|
| Re: Top 2 Salary from Each Dept without using Analytic Function [message #642097 is a reply to message #642096] |
Wed, 02 September 2015 10:57   |
John Watson
Messages: 9003 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
OK, I've debugged it for you. Is this what you mean:orclz> ed
Wrote file afiedt.buf
1 SELECT EMPNO,ENAME,SAL,RNK,DEPTNO FROM
2 (SELECT EMPNO,ENAME,SAL,
3 RANK()OVER (PARTITION BY DEPTNO ORDER BY SAL DESC) RNK,DEPTNO
4 FROM EMP)
5* WHERE RNK IN (1,2)
orclz> /
7839 KING 5000 1 10
7782 CLARK 2450 2 10
7788 SCOTT 3000 1 20
7902 FORD 3000 1 20
7698 BLAKE 2850 1 30
7499 ALLEN 1600 2 30
orclz>
|
|
|
|
|
|
| Re: Top 2 Salary from Each Dept without using Analytic Function [message #642099 is a reply to message #642093] |
Wed, 02 September 2015 11:13   |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
b.balabtech@yahoo.com wrote on Wed, 02 September 2015 21:02I want to display top 2 salary from each department without using analytic function.
I want to shift to 2nd gear without using the clutch in my car. Don't ask me why, what, how etc.
Quote:
With analytic function i can able to display.
Yeah, I see. Oh wait, are you serious? As I can't see.
Edit : Ah! I see, you posted your analytic approach(though without using code tags).
[Updated on: Wed, 02 September 2015 11:32] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Re: Top 2 Salary from Each Dept without using Analytic Function [message #642117 is a reply to message #642093] |
Wed, 02 September 2015 22:12   |
 |
aspire
Messages: 18 Registered: September 2015 Location: TVN
|
Junior Member |
|
|
I've tried in two ways....in sql
SELECT EMPNO,
ENAME,
DEPTNO,
SAL,
RNK
FROM
(SELECT EMPNO,
ENAME,
DEPTNO,
SAL,
RANK() OVER (PARTITION BY DEPTNO ORDER BY SAL DESC ) RNK
FROM EMP
)
WHERE RNK IN (1,2);
o/p :
7839 KING 10 5000 1
7782 CLARK 10 2450 2
7788 SCOTT 20 3000 1
7902 FORD 20 3000 1
7698 BLAKE 30 2850 1
7499 ALLEN 30 1600 2
In PLSQL...
DECLARE
CURSOR C1
IS
SELECT DISTINCT DEPTNO FROM EMP;
CURSOR C2(LV_DEPTNO NUMBER)
IS
SELECT ROWNUM RNK,
EMPNO,
ENAME,
DEPTNO,
SAL
FROM
(SELECT EMPNO,
ENAME,
DEPTNO,
SAL
FROM EMP
WHERE DEPTNO=LV_DEPTNO
ORDER BY SAL DESC
)
WHERE ROWNUM<3 ;
BEGIN
FOR I IN C1
LOOP
FOR J IN C2(I.DEPTNO)
LOOP
DBMS_OUTPUT.PUT_LINE(J.EMPNO||' | '||J.ENAME||' | '||I.DEPTNO||' | '||J.RNK||' | '||J.SAL);
END LOOP;
END LOOP;
END;
O/p:
7698 | BLAKE | 30 | 1 | 2850
7499 | ALLEN | 30 | 2 | 1600
7788 | SCOTT | 20 | 1 | 3000
7902 | FORD | 20 | 2 | 3000
7839 | KING | 10 | 1 | 5000
7782 | CLARK | 10 | 2 | 2450
|
|
|
|
|
|