Friday, May 17, 2013

Find Sumation Of Largest Set Of Value.


CREATING FUNCTION FOR GET SUMATION OF LARGEST VALUE
========================================================================

SQL> CREATE OR REPLACE FUNCTION LARGEST_VALUE_FROM_NUMBER
  2  (P_VALUE1 NUMBER,
  3   P_VALUE2 NUMBER,
  4   P_VALUE3 NUMBER,
  5   P_VALUE4 NUMBER,
  6   P_SELECT NUMBER
  7   )
  8   RETURN NUMBER
  9   IS
 10   V_VAL1 NUMBER;
 11   V_VAL2 NUMBER;
 12   V_VAL3 NUMBER;
 13   V_VAL4 NUMBER;
 14
 15    TYPE REC_TYPE IS RECORD (
 16      ARRY_VAL  NUMBER);
 17
 18    TYPE TAB_TYPE IS TABLE OF REC_TYPE
 19      INDEX BY BINARY_INTEGER;
 20
 21    T_VALUE TAB_TYPE;
 22  BEGIN
 23    T_VALUE(1).ARRY_VAL := P_VALUE1;
 24    T_VALUE(2).ARRY_VAL := P_VALUE2;
 25    T_VALUE(3).ARRY_VAL := P_VALUE3;
 26    T_VALUE(4).ARRY_VAL := P_VALUE4;
 27
 28    SELECT SUM(VAL)
 29    INTO V_VAL1
 30    FROM (
 31    SELECT VAL FROM (
 32    SELECT T_VALUE(1).ARRY_VAL VAL  FROM DUAL
 33    UNION ALL
 34    SELECT T_VALUE(2).ARRY_VAL VAL FROM DUAL
 35    UNION ALL
 36    SELECT T_VALUE(3).ARRY_VAL VAL FROM DUAL
 37    UNION ALL
 38    SELECT T_VALUE(4).ARRY_VAL VAL FROM DUAL)
 39    ORDER BY 1 DESC)
 40    WHERE ROWNUM <=P_SELECT;
 41
 42
 43      RETURN V_VAL1;
 44
 45    EXCEPTION
 46    WHEN OTHERS THEN
 47    RAISE_APPLICATION_ERROR(-20001,'Error In Select Max Number Mode');
 48
 49  END;
 50  /

Function created.

EXECUTING FUNCTION
==========================================================================

SQL> SELECT LARGEST_VALUE_FROM_NUMBER(10,15,20,40,3) FROM DUAL;

LARGEST_VALUE_FROM_NUMBER(10,15,20,40,3)
----------------------------------------
                                      75

SQL>

No comments:

Post a Comment