Home » SQL & PL/SQL » SQL & PL/SQL » Rank Function in PL/SQL
Rank Function in PL/SQL [message #515838] Wed, 13 July 2011 09:08 Go to next message
cdunn0804
Messages: 3
Registered: July 2011
Junior Member
Can someone tell me if there is a way when doing the Rank Function in PL/SQL to pass the field that will be ranked as an override.

SELECT rank(p_ColumnAmt) within group (order by p_ColumnNm desc) rank
INTO v_RnkNoAmt
FROM Table_name
WHERE ??????;

p_Column is the amount I am ranking
p_ColumnNm is the actual field name to Rank.

When I pass the field name with an override I do not get the correct rank back. If I run the Select with the actual field name curr_1_mth_amt, I get the correct rank.

I have about 70 different field to Rank and do not want to make a procedure for each field.

Thanks
Re: Rank Function in PL/SQL [message #515839 is a reply to message #515838] Wed, 13 July 2011 09:09 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
It would be helpful if you followed Posting Guidelines - http://www.orafaq.com/forum/t/88153/0/
Re: Rank Function in PL/SQL [message #515840 is a reply to message #515839] Wed, 13 July 2011 09:21 Go to previous messageGo to next message
cdunn0804
Messages: 3
Registered: July 2011
Junior Member
If I code the following

p_ColumnNm = 'curr_1_mth_amt';

SELECT rank(p_ColumnAmt) within group (order by p_ColumnNm desc) rank
INTO v_RnkNoAmt
FROM Table_name
WHERE ??????;
I get back the the following Rank = 12995

If I code the Selct as follows


SELECT rank(p_ColumnAmt) within group (order by Curr_1_mth_amt desc) rank
INTO v_RnkNoAmt
FROM Table_name
WHERE ??????;
I get back the the following Rank = 7540

the 7540 is the correct rank.
Re: Rank Function in PL/SQL [message #515841 is a reply to message #515840] Wed, 13 July 2011 09:26 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I have about 70 different field to Rank and do not want to make a procedure for each field.
Why not?
writing code is a 1 time activity & would result in most efficient results.

It would be helpful if you followed Posting Guidelines - http://www.orafaq.com/forum/t/88153/0/
Re: Rank Function in PL/SQL [message #515843 is a reply to message #515840] Wed, 13 July 2011 09:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
You cannot use a variable for an column or table name in a static SQL.
If you really want to do this you have to use dynamic SQL.

Regards
Michel
Re: Rank Function in PL/SQL [message #515847 is a reply to message #515843] Wed, 13 July 2011 09:38 Go to previous messageGo to next message
cdunn0804
Messages: 3
Registered: July 2011
Junior Member
Well actually 70 was not correct. I have about 240 fields to rank, so I would need a procedure for each. Seems redundant to code the same procedure with just a different field name.

Thanks I will try the dynamic SQL.
Re: Rank Function in PL/SQL [message #515849 is a reply to message #515847] Wed, 13 July 2011 09:43 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>Thanks I will try the dynamic SQL.
dynamic SQL forces hard parse for every execution & does not scale

What you gain from in reduction of one time coding effort, the application takes performance penalty for ever
Re: Rank Function in PL/SQL [message #516059 is a reply to message #515849] Thu, 14 July 2011 17:53 Go to previous message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
SCOTT@orcl_11gR2> CREATE OR REPLACE FUNCTION test_func
  2    (p_ColumnAmt IN NUMBER,
  3  	p_ColumnNm  IN VARCHAR2)
  4    RETURN	       NUMBER
  5  AS
  6    v_RnkNoAmt      NUMBER;
  7  BEGIN
  8    SELECT EXTRACTVALUE
  9  		(XMLTYPE
 10  		   (DBMS_XMLGEN.GETXML
 11  		      ('SELECT RANK (' || p_ColumnAmt || ')
 12  				 WITHIN GROUP (ORDER BY ' || p_ColumnNm || ' DESC) rank
 13  			FROM   emp')),
 14  		 '/ROWSET/ROW/RANK')
 15    INTO   v_RnkNoAmt
 16    FROM   DUAL;
 17    RETURN v_RnkNoAmt;
 18  END test_func;
 19  /

Function created.

SCOTT@orcl_11gR2> SHOW ERRORS
No errors.
SCOTT@orcl_11gR2> SELECT test_func (20, 'DEPTNO') FROM DUAL
  2  /

TEST_FUNC(20,'DEPTNO')
----------------------
                     8

1 row selected.

SCOTT@orcl_11gR2> SELECT test_func (7900, 'EMPNO') FROM DUAL
  2  /

TEST_FUNC(7900,'EMPNO')
-----------------------
                      3

1 row selected.

SCOTT@orcl_11gR2> 

Previous Topic: Dash (#) in a formatted column
Next Topic: SCHEDULING IN ORACLE PL SQL(4 Merged)
Goto Forum:
  


Current Time: Sat Aug 23 21:35:54 CDT 2025