Home » SQL & PL/SQL » SQL & PL/SQL » Top 2 Salary from Each Dept without using Analytic Function (Oracle 11g,windows 7)
Top 2 Salary from Each Dept without using Analytic Function [message #642093] Wed, 02 September 2015 10:32 Go to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
I want to display top 2 salary from each department without using analytic function.
With analytic function i can able to display.
Re: Top 2 Salary from Each Dept without using Analytic Function [message #642094 is a reply to message #642093] Wed, 02 September 2015 10:37 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
Welcome to the forum. Please read our OraFAQ Forum Guide and How to use [code] tags and make your code easier to read

Is this a college homework assignment? If so, you need to show the SQL that you have tried so far. And in any case, can you show the SQL you wrote using analytics, to make the problem and the necessary result clear? Just run the query in SQL*Plus, and use copy/paste to copy the query and result here. Please be sure to enclose it in [code] tags.
Re: Top 2 Salary from Each Dept without using Analytic Function [message #642095 is a reply to message #642094] Wed, 02 September 2015 10:49 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
--Using Analytic function
[SELECT EMP_ID,EMP_NAME,SALARY,RNK FROM
(SELECT EMP_ID,EMP_NAME,SALARY,
RANK()OVER (PARTITION BY EMP_DEP_ID ORDER BY SALARY DESC) RNK
FROM EMP)
WHERE RNK IN (1,2);]
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 #642098 is a reply to message #642093] Wed, 02 September 2015 10:58 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
this is definitely a college homework
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1212501913138
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 Go to previous messageGo to next message
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:02
I 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 #642100 is a reply to message #642097] Wed, 02 September 2015 11:24 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
Yes..This is correct. But i want to display without using rank() and dense_rank() funtion...Is this possible???
Re: Top 2 Salary from Each Dept without using Analytic Function [message #642101 is a reply to message #642100] Wed, 02 September 2015 11:30 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
Is this possible???


Yes.

Re: Top 2 Salary from Each Dept without using Analytic Function [message #642102 is a reply to message #642100] Wed, 02 September 2015 11:31 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Yes, it is possible.
Re: Top 2 Salary from Each Dept without using Analytic Function [message #642103 is a reply to message #642101] Wed, 02 September 2015 11:31 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
How???
Re: Top 2 Salary from Each Dept without using Analytic Function [message #642104 is a reply to message #642103] Wed, 02 September 2015 11:33 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

I will not post some bad solutions unless you explain why you don't want to use analytic functions.

Re: Top 2 Salary from Each Dept without using Analytic Function [message #642105 is a reply to message #642103] Wed, 02 September 2015 11:43 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
This is clearly a college homework question. The operative syllable is "work": no-one is going to write it for you, because that would be cheating.

Are you allowed to use a row limit clause?
http://docs.oracle.com/database/121/SQLRF/statements_10002.htm#BABHFGAA
this, syntactically, does not use an analytic function.

And again, what SQL have you tried so far?
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 Go to previous messageGo to next message
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
Re: Top 2 Salary from Each Dept without using Analytic Function [message #642120 is a reply to message #642117] Thu, 03 September 2015 00:51 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

And what did you try in SQL and without analytic functions?

Previous Topic: migrate View from SQL to Oracle
Next Topic: Constraint problem for flag
Goto Forum:
  


Current Time: Tue Jul 28 22:26:04 CDT 2026