Oracle repeated function calls in select and where clause - sql

I have the following Oracle SQL query
select /*+ORDERED */ payr.payroll_name payroll_name,
nvl(papf.employee_number, papf.npw_number) employee_number,
nvl
(
xxpay_util.safe_to_number
(
xxpay_util_element.get_run_result
(
p_business_group_id => paaf.business_group_id, -- in number
p_payroll_id => paaf.payroll_id, -- in number
p_payroll_action_id => null, -- in number,
p_start_period => mon.real_month_start, -- in date
p_end_period => mon.real_month_end, -- in date
p_element_name_regex => '^Disability Cover ER$', -- in varchar2 '^Disability Cover ER Opt Out$'
p_input_value => 'Pay Value', -- in varchar2
p_assignment_id => paaf.assignment_id, -- in number
p_aggregation => 'sum', -- in varchar2
p_round => 2 -- in number default 2
)
),
0
) disability_benefit,
nvl
(
xxpay_util.safe_to_number
(
xxpay_util_element.get_run_result
(
p_business_group_id => paaf.business_group_id, -- in number
p_payroll_id => paaf.payroll_id, -- in number
p_payroll_action_id => null, -- in number,
p_start_period => mon.real_month_start, -- in date
p_end_period => mon.real_month_end, -- in date
p_element_name_regex => '^Death Cover ER Opt Out$', -- in varchar2
p_input_value => 'Pay Value', -- in varchar2
p_assignment_id => paaf.assignment_id, -- in number
p_aggregation => 'sum', -- in varchar2
p_round => 2 -- in number default 2
)
),
0
) death_cover,
nvl
(
xxpay_util.safe_to_number
(
xxpay_util_element.get_run_result
(
p_business_group_id => paaf.business_group_id, -- in number
p_payroll_id => paaf.payroll_id, -- in number
p_payroll_action_id => null, -- in number,
p_start_period => mon.real_month_start, -- in date
p_end_period => mon.real_month_end, -- in date
p_element_name_regex => '^Funeral Cover ER$', -- in varchar2 '^Funeral Cover ER Opt Out$'
p_input_value => 'Pay Value', -- in varchar2
p_assignment_id => paaf.assignment_id, -- in number
p_aggregation => 'sum', -- in varchar2
p_round => 2 -- in number default 2
)
),
0
) funeral_cover,
ppos.actual_termination_date termination_date
from (
select ptp.payroll_id,
min(ptp.start_date) real_month_start,
max(ptp.end_date) real_month_end
from per_time_periods ptp
where decode(ptp.prd_information_category, 'ZA', to_char(ptp.pay_advice_date, 'yyyymm'), to_char(ptp.regular_payment_date, 'yyyymm')) = '201612'
group by ptp.payroll_id
) mon,
pay_all_payrolls_f payr,
per_all_assignments_f paaf,
per_periods_of_service ppos,
per_people_f papf,
per_business_groups pbg,
hr_all_organization_units haou,
per_jobs pj,
pay_cost_allocation_keyflex pcak
where payr.payroll_id = mon.payroll_id
and mon.real_month_end between payr.effective_start_date and payr.effective_end_date
and paaf.payroll_id = mon.payroll_id
and paaf.assignment_type = 'E'
and paaf.primary_flag = 'Y'
and paaf.effective_start_date =
(
select max(paaf2.effective_start_date)
from per_all_assignments_f paaf2
where paaf2.assignment_id = paaf.assignment_id
and paaf2.assignment_type = 'E'
and paaf2.primary_flag = 'Y'
and paaf2.payroll_id = mon.payroll_id
and mon.real_month_start <= paaf2.effective_end_date
and mon.real_month_end >= paaf2.effective_start_date
)
and ppos.period_of_service_id = paaf.period_of_service_id
and mon.real_month_start <= nvl(ppos.actual_termination_date, to_date('31/12/4712', 'dd/mm/yyyy'))
and mon.real_month_end >= ppos.date_start
and papf.person_id = paaf.person_id
and papf.effective_start_date =
(
select max(papf2.effective_start_date)
from per_all_people_f papf2
where papf2.person_id = paaf.person_id
and mon.real_month_start <= papf2.effective_end_date
and mon.real_month_end >= papf2.effective_start_date
)
and pbg.business_group_id = paaf.business_group_id
and haou.organization_id (+) = paaf.organization_id
and pj.job_id (+) = paaf.job_id
and pcak.cost_allocation_keyflex_id (+) = haou.cost_allocation_keyflex_id
and
(
nvl
(
xxpay_util.safe_to_number
(
xxpay_util_element.get_run_result
(
p_business_group_id => paaf.business_group_id, -- in number
p_payroll_id => paaf.payroll_id, -- in number
p_payroll_action_id => null, -- in number,
p_start_period => mon.real_month_start, -- in date
p_end_period => mon.real_month_end, -- in date
p_element_name_regex => '^Disability Cover ER$', -- in varchar2 -- Disability Cover ER Opt Out
p_input_value => 'Pay Value', -- in varchar2
p_assignment_id => paaf.assignment_id, -- in number
p_aggregation => 'sum', -- in varchar2
p_round => 2 -- in number default 2
)
),
0
)
+
nvl
(
xxpay_util.safe_to_number
(
xxpay_util_element.get_run_result
(
p_business_group_id => paaf.business_group_id, -- in number
p_payroll_id => paaf.payroll_id, -- in number
p_payroll_action_id => null, -- in number,
p_start_period => mon.real_month_start, -- in date
p_end_period => mon.real_month_end, -- in date
p_element_name_regex => '^Death Cover ER Opt Out$', -- in varchar2
p_input_value => 'Pay Value', -- in varchar2
p_assignment_id => paaf.assignment_id, -- in number
p_aggregation => 'sum', -- in varchar2
p_round => 2 -- in number default 2
)
),
0
)
+
nvl
(
xxpay_util.safe_to_number
(
xxpay_util_element.get_run_result
(
p_business_group_id => paaf.business_group_id, -- in number
p_payroll_id => paaf.payroll_id, -- in number
p_payroll_action_id => null, -- in number,
p_start_period => mon.real_month_start, -- in date
p_end_period => mon.real_month_end, -- in date
p_element_name_regex => '^Funeral Cover ER$', -- in varchar2 -- Funeral Cover ER Opt Out
p_input_value => 'Pay Value', -- in varchar2
p_assignment_id => paaf.assignment_id, -- in number
p_aggregation => 'sum', -- in varchar2
p_round => 2 -- in number default 2
)
),
0
)
) != 0
and nvl
(
xxpay_util_element.get_element_entry
(
p_business_group_id => paaf.business_group_id, -- in number
p_start_period => mon.real_month_start, -- in date
p_end_period => mon.real_month_end, -- in date
p_element_name_regex => '^Retirement Fund Opt In$', -- in varchar2
p_input_value => 'Opt In or Out', -- in varchar2
p_assignment_id => paaf.assignment_id, -- in number
p_aggregation => 'none', -- in varchar2
p_round => 0 -- in number default 2
),
'$XXX$'
) in ('NC', 'OUT')
and exists
(
select 1
from pay_payroll_actions ppap,
pay_assignment_actions paap,
pay_action_interlocks pai,
pay_assignment_actions paar,
pay_payroll_actions ppar,
per_time_periods ptp
where ppap.action_type in ('P', 'U')
and ppap.action_status = 'C'
and paap.payroll_action_id = ppap.payroll_action_id
and paap.action_status = 'C'
and pai.locking_action_id = paap.assignment_action_id
and paar.assignment_action_id = pai.locked_action_id
and ppar.payroll_action_id = paar.payroll_action_id
and ppar.action_type in ('R', 'Q')
and ptp.time_period_id = ppar.time_period_id
and decode(ptp.prd_information_category, 'ZA', to_char(ptp.pay_advice_date, 'yyyymm'), to_char(ptp.regular_payment_date, 'yyyymm')) = '201612'
and paap.assignment_id = paaf.assignment_id
and paar.assignment_id = paaf.assignment_id
)
order by pcak.segment2, papf.employee_number, paaf.assignment_number
Is there a way to rewrite the query so that the function calls from the select clause (xxpay_util_element.get_run_result) that is repeated in the where clause are not repeated and only called once?

you can try creating this sql as subquery and add where clause in parent query :
Select * from (
select
xxpay_util_element.get_run_result as get_run_result_1, --only fetch values here
.......
from .....
)
where get_run_result_1 ......... --add where clause here

Related

Group by on multiple subqueries

I'm new to Oracle SQL and am still learning, I'm trying to work out what GROUP BY I need to use.
The subquery works by itself:
SELECT TO_CHAR(CREATE_DATE_TIME, 'DD-MON-YYYY') "DTTM"
, CASE_NBR
, COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID) "REF_FIELD_1"
, COALESCE(PT.REF_FIELD_2, LPN.ASN_ID || LPN.ITEM_ID) "REF_FIELD_2"
FROM PIX_TRAN PT, LPN
WHERE ( ( PT.TRAN_TYPE = '300'
AND PT.TRAN_CODE = '01'
AND PT.ACTN_CODE = '20' )
OR ( PT.TRAN_TYPE = '300'
AND PT.TRAN_CODE = '04'
AND PT.ACTN_CODE = '21' ) )
AND SUBSTR(COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID), 1, INSTR(COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID), '_', 1)) != 'Return_'
AND PT.CASE_NBR = LPN.TC_LPN_ID (+)
AND PT.WHSE = 'DCV'
AND TRUNC(CREATE_DATE_TIME) = TRUNC(SYSDATE)
But when I try to add it as a subquery with a GROUP BY, I can't seem to work out what the correct GROUP BY should be?
SELECT 'PO Lines/LPNs Putaway' AS "FACILITY_ACTIVITY"
, TRUNC DTTM AS "CREATED"
, COUNT(DISTINCT REF_FIELD_1 || REF_FIELD_2)|| '/'|| COUNT(DISTINCT CASE_NBR) "Total"
FROM (
SELECT TO_CHAR(CREATE_DATE_TIME, 'DD-MON-YYYY') "DTTM"
, CASE_NBR
, COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID) "REF_FIELD_1"
, COALESCE(PT.REF_FIELD_2, LPN.ASN_ID || LPN.ITEM_ID) "REF_FIELD_2"
FROM PIX_TRAN PT, LPN
WHERE ( ( PT.TRAN_TYPE = '300'
AND PT.TRAN_CODE = '01'
AND PT.ACTN_CODE = '20' )
OR ( PT.TRAN_TYPE = '300'
AND PT.TRAN_CODE = '04'
AND PT.ACTN_CODE = '21' ) )
AND SUBSTR(COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID), 1, INSTR(COALESCE(PT.REF_FIELD_1, LPN.TC_ASN_ID), '_', 1)) != 'Return_'
AND PT.CASE_NBR = LPN.TC_LPN_ID (+)
AND PT.WHSE = 'DCV'
AND TRUNC(CREATE_DATE_TIME) = TRUNC(SYSDATE)
)
GROUP BY TRUNC(DTTM);
I've tried the following GROUP BY's
GROUP BY TRUNC(DTTM)
ERROR - "FROM Keyword not found where expected"
GROUP BY TRUNC(TO_CHAR(CREATE_DATE_TIME, 'DD-MON-YYYY'))
with changing the select clause to
TRUNC(TO_CHAR(CREATE_DATE_TIME, 'DD-MON-YYYY')) AS "CREATED"
ERROR - "CREATE_DATE_TIME" invalid identifier
GROUP BY TRUNC(CREATE_DATE_TIME)
with changing the select clause to
TRUNC(CREATE_DATE_TIME) AS "CREATED"
ERROR - "CREATE_DATE_TIME" invalid identifier
Can someone please point out what I'm missing?
I formatted your queries which makes it easy to see the issue
Original
SELECT
to_char(create_date_time, 'DD-MON-YYYY') "DTTM",
case_nbr,
coalesce(pt.ref_field_1, lpn.tc_asn_id) "REF_FIELD_1",
coalesce(pt.ref_field_2, lpn.asn_id || lpn.item_id) "REF_FIELD_2"
FROM
pix_tran pt,
lpn
WHERE
( ( pt.tran_type = '300'
AND pt.tran_code = '01'
AND pt.actn_code = '20' )
OR ( pt.tran_type = '300'
AND pt.tran_code = '04'
AND pt.actn_code = '21' ) )
AND substr(coalesce(pt.ref_field_1, lpn.tc_asn_id),
1,
instr(coalesce(pt.ref_field_1, lpn.tc_asn_id),
'',
1)) != 'Return'
AND pt.case_nbr = lpn.tc_lpn_id (+)
AND pt.whse = 'DCV'
AND trunc(create_date_time) = trunc(sysdate)
Inline view
select 'PO Lines/LPNs Putaway' as "FACILITY_ACTIVITY",
trunc dttm AS "CREATED" , COUNT(DISTINCT REF_FIELD_1 || REF_FIELD_2)|| '/'|| COUNT(DISTINCT CASE_NBR) "Total" FROM
(
select to_char(
create_date_time,
'DD-MON-YYYY'
) "DTTM",
case_nbr,
coalesce(
pt.ref_field_1,
lpn.tc_asn_id
) "REF_FIELD_1",
coalesce(
pt.ref_field_2,
lpn.asn_id || lpn.item_id
) "REF_FIELD_2"
from pix_tran pt,
lpn
where ( ( pt.tran_type = '300'
and pt.tran_code = '01'
and pt.actn_code = '20' )
or ( pt.tran_type = '300'
and pt.tran_code = '04'
and pt.actn_code = '21' ) )
and substr(
coalesce(
pt.ref_field_1,
lpn.tc_asn_id
),
1,
instr(
coalesce(
pt.ref_field_1,
lpn.tc_asn_id
),
'',
1
)
) != 'Return'
and pt.case_nbr = lpn.tc_lpn_id (+)
and pt.whse = 'DCV'
and trunc(create_date_time) = trunc(sysdate)
)
group by trunc(dttm);
You are missing the brackets on your TRUNC, and since your DTTM is a string, the use of trunc at all is probably not appropriate.
I would move the TRUNC inside the subquery (it will reduce the datetime to a date) and then just group by DTTM

Postgresql select in variable

I have two select
do $$
declare
uid text := 'b5974eec-7078-4ae7-923c-36af96905423';
my_output record;
Begin
-- Czas konwersji jednego ekstratku
SELECT sha256, (
SELECT timestamp
FROM public.tmask_mda02
WHERE uuid = uid
AND import = 'COMPLETED'
AND params = 'sha256 in')::timestamp - (
SELECT timestamp
FROM public.tmask_mda02
WHERE uuid = uid
AND export = 'COMPLETED'
AND params = 'sha256 out')::timestamp AS Interval
INTO my_output
FROM public.tmask_mda02
WHERE uuid = uid
AND params = 'sha256 out';
RAISE NOTICE 'Dlugosc_przetwarzania: %',my_output;
end $$;
Output is:
Dlugosc_przetwarzania:
(89b253f28435aea23fc68c042066e988d236f2f312583255c0352ea512701daf,-00:00:01.145)
SELECT DISTINCT uuid FROM public.tmask_mda02 WHERE export = 'COMPLETED' AND timestamp::text LIKE '2021-09-22%';
Output:
"02de41c7-488f-4dc9-bb33-b76b05426cde"
"0b82e409-e37f-4b34-a90a-c5b57d63d759"
"19d38112-efb0-4f67-97aa-493d0cf9a1a4"
"1e8e5745-6885-4e18-89a1-3c841efd48ac"
"223cfb85-b8eb-4ea1-8f14-fbd565f68344"
"27fcaadc-f487-4bfe-bee8-00b98375844b"
"311e0cc8-f398-4134-855f-b64631254af5"
"31f856fb-c814-4214-9612-1703f247de2b"
I need use select in variable uid:
do $$
declare
uid text := (SELECT DISTINCT uuid FROM public.tmask_mda02 WHERE export = 'COMPLETED' AND timestamp::text LIKE '2021-09-22%');
my_output record;
Begin
-- Czas konwersji jednego ekstratku
SELECT sha256, (
SELECT timestamp
FROM public.tmask_mda02
WHERE uuid = uid
AND import = 'COMPLETED'
AND params = 'sha256 in')::timestamp - (
SELECT timestamp
FROM public.tmask_mda02
WHERE uuid = uid
AND export = 'COMPLETED'
AND params = 'sha256 out')::timestamp AS Interval
INTO my_output
FROM public.tmask_mda02
WHERE uuid = uid
AND params = 'sha256 out';
RAISE NOTICE 'Dlugosc_przetwarzania: %',my_output;
end $$;

Issue with select query: ORA-01861: literal does not match format string

I am facing issues while exeucting the below query as "ORA-01861: literal does not match format string". Not able to identify the actual issue.
select
*
from(
select
a.*,
rownum rnum
from(
SELECT
E1."BAMEVENT" AS "BAMEVENT",
E1."BCRMORDERID" AS "BCRMORDERID",
E1."COMPLETIONTIME" AS "COMPLETIONTIME",
E1."CONTACT" AS "CONTACT",
E1."CREATIONTIME" AS "CREATIONTIME",
E1."CURRENTOWNERID" AS "CURRENTOWNERID",
E1."CURRENTOWNERNAME" AS "CURRENTOWNERNAME",
E1."CUSTOMERNAME" AS "CUSTOMERNAME",
E1."CUSTOMERSEGMENT" AS "CUSTOMERSEGMENT",
E1."CUSTOMERSUBSEGMENT" AS "CUSTOMERSUBSEGMENT",
E1."CUSTOMERID" AS "CUSTOMERID",
E1."CUSTOMERREGION" AS "CUSTOMERREGION",
E1."ESCLATIONLEVELFIRED" AS "ESCALATIONLEVELFIRED",
E1."ESTIMATECOMPLETIONDATE" AS "ESTIMATECOMPLETETIME",
E1."OVERALLESTIMATECOMPLETIONDATE" AS "ESTIMATEDCLOSUREDATE",
E1."EVENTTYPE" AS "EVENTTYPE",
E1."EVENTWEIGHT" AS "EVENTWEIGHT",
E1."MODIFIEDBY" AS "MODIFIEDBY",
E1."MODIFIEDDATE" AS "MODIFIEDDATE",
E1."ORDERNUMBER" AS "ORDERNUMBER",
E1."ORDERSTATUS" AS "ORDERSTATUS",
E1."OWNER" AS "OWNER",
E1."PARTYID" AS "PARTYID",
E1."PARTYNAME" AS "PARTYNAME",
E1."PROCESSINSTANCEID" AS "PROCESSINSTANCEID",
E1."PRODUCTDESC" AS "PRODUCTDESCRIPTION",
E1."PRODUCTOFFERINGNAME" AS "PRODUCTOFFERING",
E1."REQUESTID" AS "REQUESTID",
E1."REQUESTEDDATE" AS "REQUESTEDDATE",
E1."REQUESTEDSYSTEM" AS "REQUESTEDSYSTEM",
E1."REVERSALREASON" AS "REVERSALREASON",
E1."SLABREACHED" AS "SLABREACHED",
E1."SMARTSEARCH" AS "SMARTSEARCH",
E1."TICKETNUMBER" AS "TICKETNUMBER",
E1."TRANSACTIONID" AS "TRANSACTIONID",
E1."PRODUCTIDENTIFIER" AS "PRODUCTIDENTIFIER",
C2."BUSINESSSEGMENT" AS "BUSINESSSEGMENT",
E1."PRODUCTIDENTIFIER" AS "COLUMN",
E1."ORDERCHANNEL" AS "ORDERCHANNEL",
E1."OVERALLPROGRESSPRCNT" AS "OVERALLPROGRESSPRCNT",
E1."PROCESSINSTANCEPRCNT" AS "PROCESSINSTANCEPRCNT",
O3."BCRMREQUESTTYPE" AS "BCRMREQUESTTYPE",
O3."OPRODUCTDETAIL_PRODUCTCATEGORY" AS "OPRODUCTDETAILPRODUCTCATEGOR",
O3."OPRODUCTDETAIL_PRODUCTLINE" AS "OPRODUCTDETAILPRODUCTLINE",
O3."OPRODUCTDETAIL_PRODUCTOFFERING" AS "OPRODUCTDETAILPRODUCTOFFERIN",
R4."CBCMREQUESTTYPE" AS "CBCMREQUESTTYPE",
R4."ORDERREFERENCENUMBER" AS "ORDERREFERENCENUMBER"
FROM
"BAMSPAPP"."ETOE_ORDER_TRACKER" E1,
"BAMSPAPP"."CUST_DETAILS" C2,
"BAMSPAPP"."ORDER_LINE_ITEMS_DETAILS" O3,
"BAMSPAPP"."REQUEST_DETAILS" R4
WHERE
(
E1."PROCESSINSTANCEID" = C2."PROCESSINSTANCEID"(+)
)
AND (
E1."PROCESSINSTANCEID" = O3."PROCESSINSTANCEID"(+)
)
AND (
E1."PROCESSINSTANCEID" = R4."PROCESSINSTANCEID"(+)
)
AND E1.CREATIONTIME >= '2019-02-01T00:00:00'
AND E1.CREATIONTIME < = '2019-05-06T00:00:00'
AND (
(
(
E1.ORDERSTATUS = 'Closed'
or E1.ORDERSTATUS = 'Cancelled'
)
and E1.PRODUCTIDENTIFIER = 'Mobile'
and NOT(E1.COMPLETIONTIME < SYSDATE -10)
)
OR (
(
E1.ORDERSTATUS = 'Closed'
or E1.ORDERSTATUS = 'Cancelled'
)
and (
E1.PRODUCTIDENTIFIER IS NULL
or E1.PRODUCTIDENTIFIER != 'Mobile'
)
and NOT(E1.COMPLETIONTIME < SYSDATE -30)
)
OR (
E1.ORDERSTATUS != 'Closed'
and E1.ORDERSTATUS != 'Cancelled'
)
)
order by
E1.CREATIONTIME
) a
where
rownum <= 5000
)
where
rnum >= 0
The error comes from comparing dates with character strings. Assuming CREATIONDATE is a date type field (which it should be) then the error is in the following conditions in your where clause:
E1.CREATIONTIME >= '2019-02-01T00:00:00'
AND E1.CREATIONTIME < = '2019-05-06T00:00:00'
You can solve it as follows:
E1.CREATIONTIME >= to_date('2019-02-01', 'YYYY-MM-DD')
AND E1.CREATIONTIME <= to_date('2019-05-06', 'YYYY-MM-DD')
Or from Oracle 11g onwards:
E1.CREATIONTIME >= date '2019-02-01'
AND E1.CREATIONTIME <= date '2019-05-06'
If however, these fields are character strings (varchar2), then the problem will be with the following condition:
NOT(E1.COMPLETIONTIME < SYSDATE -10)
Then you would need to fix it like this:
NOT(to_date(E1.COMPLETIONTIME, 'YYYY-MM-DD"T"HH24:MI:SS') < SYSDATE -10)
...where the second argument passed to the to_date function must represent the exact format of the field's content.

Function in database for view current date fit this condition

In this condition users can visit in a schedule.
How me to create a function in oracle database that have logic are:
one month once visit (f1)
two month once visit (f2)
one week once visit (f4)
one week twice visit(f8).
So we can view data in current date fit with that condition.
Example in database with table name is mst_callplan:
cust_no : 201, sales_no:001, frequent: f1, day: monday
cust_no : 202, sales_no:001, frequent: f8, day: friday
cust_no : 203, sales_no:001, frequent: f2, day: wednesday
cust_no : 204, sales_no:001, frequent: f1, day: monday
cust_no,sales_no,frequent,and day is name of column.
From database will view row of table fit with that condition in current date. The data shown automatic when current date is change.
For the example result is: null (for this current date : 25/04/2017)
Help me for the function in database. how to create the function fit that condition.
function in that case is:
FUNCTION get_data_sales(pi_trxn_dt IN DATE)
RETURN slm_type_mst_callplan_tab IS
l_slm_type_mst_callplan_rec slm_type_mst_callplan_rec;
l_slm_type_mst_callplan_tab slm_type_mst_callplan_tab := slm_type_mst_callplan_tab();
l_return PLS_INTEGER;
ex_set_date_lang EXCEPTION;
CURSOR cur_data IS
SELECT cust_id
,sales_id
,mst_freq
,mst_day
,start_date
,end_date
,week_no
FROM mst_callplan
WHERE (upper(mst_freq) = 'F1' and week_no=4 AND
MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
,pi_end_dt => pi_trxn_dt)
,4) = 0 AND upper(TRIM(mst_day)) =
upper(TRIM(to_char(pi_trxn_dt,'Day'))))
OR
(upper(mst_freq) = 'F1'and week_no=3 AND
MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
,pi_end_dt => pi_trxn_dt)
,4) = 3 AND upper(TRIM(mst_day)) =
upper(TRIM(to_char(pi_trxn_dt,'Day'))))
OR
(upper(mst_freq) = 'F1'and week_no=2 AND
MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
,pi_end_dt => pi_trxn_dt)
,4) = 2 AND upper(TRIM(mst_day)) =
upper(TRIM(to_char(pi_trxn_dt,'Day'))))
OR
(upper(mst_freq) = 'F1'and week_no=1 AND
MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
,pi_end_dt => pi_trxn_dt)
,4) = 1 AND upper(TRIM(mst_day)) =
upper(TRIM(to_char(pi_trxn_dt,'Day'))))
OR
(upper(mst_freq) = 'F2' and week_no=2 AND
MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
,pi_end_dt => pi_trxn_dt)
,2) = 0 AND upper(TRIM(mst_day)) =
upper(TRIM(to_char(pi_trxn_dt
,'Day'))))
OR
(upper(mst_freq) = 'F2' and week_no=1 AND
MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
,pi_end_dt => pi_trxn_dt)
,2) = 1 AND upper(TRIM(mst_day)) =
upper(TRIM(to_char(pi_trxn_dt,'Day'))))
OR (upper(mst_freq) = 'F4' AND
upper(TRIM(mst_day)) =
upper(TRIM(to_char(pi_trxn_dt
,'Day'))))
OR (upper(mst_freq) = 'F8' AND
upper(TRIM(mst_day)) =
upper(TRIM(to_char(pi_trxn_dt
,'Day'))));
BEGIN
set_date_language(po_return => l_return);
IF l_return <> 0 THEN
RAISE ex_set_date_lang;
END IF;
FOR rec_dt IN cur_data
LOOP
l_slm_type_mst_callplan_tab.extend;
l_slm_type_mst_callplan_tab(l_slm_type_mst_callplan_tab.last) := slm_type_mst_callplan_rec(rec_dt.cust_id
,rec_dt.sales_id
,rec_dt.mst_freq
,rec_dt.mst_day
,rec_dt.start_date
,rec_dt.end_date
,rec_dt.week_no);
END LOOP;
RETURN l_slm_type_mst_callplan_tab;
EXCEPTION
WHEN ex_set_date_lang THEN
dbms_output.put_line('Error set date languange');
WHEN OTHERS THEN
l_slm_type_mst_callplan_tab.delete;
END get_data_sales;

nested clob xmlelemnt oracle sql select

I get this error when trying to run this sql scrip, I've searched alot for converting clob to varchar2 but useless
ORA-00932: inconsistent datatypes: expected - got CLOB
SELECT XMLELEMENT("ejada:PrivilegeResourcesList",XMLATTRIBUTES(NOENTITYESCAPING GET_MCR_PARAMETER('xmlns:core') AS "xmlns:core", GET_MCR_PARAMETER('xmlns:ejada') AS "xmlns:ejada"),
XMLAGG(XMLELEMENT("ejada:PrivilegeResourcesInfo",XMLELEMENT("ejada:PrivilegeRec",
XMLELEMENT("ejada:FuncId","F_CODE"),
XMLELEMENT("ejada:SCId","PS_CHANNEL_ID"),
XMLELEMENT("ejada:SrcResourcesList","SRCLIST"),
XMLELEMENT("ejada:TargResourcesList","TRGLIST"),
XMLELEMENT("ejada:Status","PS_STATUS")
)))) .GETCLOBVAL()
INTO P_PRIVILEGE_RESOURCES_LIST
from (
SELECT distinct
F_CODE, PS_CHANNEL_ID,
(
SELECT XMLAGG(XMLELEMENT ("core:ResourceInfo",XMLELEMENT ("core:ResourceId",
DECODE(PR_RESOURCE_TYPE_ID,
'1', XMLELEMENT("core:AcctId",XMLELEMENT("core:AcctId","PR_RESOURCE_VALUE")),
'2', XMLELEMENT("core:BillId",XMLELEMENT("core:BillNum","PR_RESOURCE_VALUE")),
'3', XMLELEMENT("core:BenId",XMLELEMENT("core:BenCode","PR_RESOURCE_VALUE"))
))
,XMLELEMENT("core:ResourceType","PRT_RESOUCE_TYPE_NAME_E")
)).GETCLOBVAL() FROM(SELECT PR_RESOURCE_TYPE_ID, PR_RESOURCE_VALUE, PRT_RESOUCE_TYPE_NAME_E FROM PERMISSION_RESOURCES, PERM_RESOURCE_TYPES WHERE PR_PERMISSION_ID = P.PS_PERMISSION_ID AND PR_USAGE_TYPE = 'S' AND PR_RESOURCE_TYPE_ID = PRT_RESOURCE_TYPE_ID)
) SRCLIST,
(
SELECT XMLAGG(XMLELEMENT ("core:ResourceInfo",XMLELEMENT ("core:ResourceId",
DECODE(PR_RESOURCE_TYPE_ID,
'1', XMLELEMENT("core:AcctId",XMLELEMENT("core:AcctId","PR_RESOURCE_VALUE")),
'2', XMLELEMENT("core:BillId",XMLELEMENT("core:BillNum","PR_RESOURCE_VALUE")),
'3', XMLELEMENT("core:BenId",XMLELEMENT("core:BenCode","PR_RESOURCE_VALUE"))
))
,XMLELEMENT("core:ResourceType","PRT_RESOUCE_TYPE_NAME_E")
)).GETCLOBVAL() FROM(SELECT PR_RESOURCE_TYPE_ID, PR_RESOURCE_VALUE, PRT_RESOUCE_TYPE_NAME_E FROM PERMISSION_RESOURCES, PERM_RESOURCE_TYPES WHERE PR_PERMISSION_ID = P.PS_PERMISSION_ID AND PR_USAGE_TYPE = 'D' AND PR_RESOURCE_TYPE_ID = PRT_RESOURCE_TYPE_ID)
) TRGLIST,
PS_STATUS
FROM FUNCTIONS F , SERVICES S , PERMISSIONS P
WHERE PS_STATUS = 'A'
--AND PS_COMP_TYPE_ID = v_component_type
-- AND Ps_COMPONENT_ID = v_component_id
AND PS_ROLE_ID = P_ROLE_ID
AND PS_ROLE_ID IN (SELECT R_ROLE_ID
FROM ROLES
WHERE R_ROLE_ID=P_ROLE_ID AND
R_COMP_TYPE_ID = 'ORGZ' AND
R_COMPONENT_ID= P_ORG_ID)
AND PS_FUNCTION_ID = F_code
AND F_SERVICE_ID = S_SERVICE_ID
-- AND PS_PERMISSION_ID = PR_PERMISSION_ID
ORDER BY F_CODE
) ;