Rank Function in PL/SQL [message #515838] |
Wed, 13 July 2011 09:08  |
 |
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 #516059 is a reply to message #515849] |
Thu, 14 July 2011 17:53  |
 |
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>
|
|
|