Requirement
The table XYZ
contains the column p_type
which contains the column name of PQR table . Now here I am fetching a record from the XYZ
table and sum(amount)
on the basis of the group by p_type
which is fetched from the record from table XYZ
. I have used partition by because I also want t_id
and not in group by. Now I take the records fetched from PQR
table and compare with a record of the XYZ
table, and for each record fetched from PQR
table, sum(amount)
is compared with a record of XYZ table.
Question
I have fetched the xyz_id
and t_id
in the nested table, but I want to take this in cursor, IS there any way to do this so that I can select these record in one go.
The probable solution I found is to take a temporary table, but if I dont want to use a temp table, then what will be the solution.
DECLARE
type queryCursor is ref cursor;
v_ref queryCursor;
v_query queryCursor;
v_t_id pqr.t_id%TYPE;
v_total NUMBER (38);
CURSOR varcursor
IS
SELECT abc_id,p_type,p_amount
FROM xyz;
v_temp VARCHAR2 (4000);
v_temp1 VARCHAR2 (200);
v_temp2 VARCHAR2 (200);
TYPE XYZ_table IS TABLE OF XYZ%ROWTYPE;
v_XYZ XYZ_table :=XYZ_table();
TYPE PQR_table IS TABLE OF PQR%ROWTYPE;
v_PQR PQR_table :=XYZ_table();
BEGIN
FOR varcursor_rec IN varcursor
LOOP
v_temp1 := varcursor_rec.p_type; --p_type contains column name of the pqr table
v_temp :=
'SELECT T_ID,SUM(AMOUNT) OVER (PARTITION BY '
|| v_temp1
|| ' ORDER BY '
|| v_temp1
|| ') total from pqr
||';
OPEN v_ref FOR v_temp;
LOOP
FETCH V_REF into v_t_id,v_total;
exit when V_REF%notfound;
IF varcursor_rec.p_amount <v_total
THEN
counter:=counter+1;
v_XYZ.EXTEND;
v_PQR.EXTEND;
v_XYZ(counter).xyz_id:=varcursor_rec.xyz_id;
v_PQR(counter).t_id:=varcursor_rec.t_id;
--HOW CAN I FETCH THE
END IF;
END LOOP;
END LOOP;
END;
/
XYZ Table:
xyz_id p_type p_amount
============================================================
p_1 p_id 100000
p_2 p_id,t_status 100
PQR Table:
T_id p_id amount t_status
=================================================
1 E1 100 open
2 E2 200 open
3 E1 200 close
4 E2 300 open
5 E1 100 close