Postgresql select in variable - sql

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 $$;

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

SQL Update query with condition

Is it better that I just use the where for update query with conditions or there is a better way to do?
Below is a sample of my SQL query which I wanted to update if the Order_ID = 10 plus payment_check is N or response_msg is null, kindly correct me if my query is wrong.
UPDATE dbo.sample1
SET Payment_Generated = 'Y',
Transaction_ID = '123',
Response_Msg = 'ok',
Response_Code = '1',
Created_on = '2020-05-29T11:29:30'
WHERE Order_ID = '10' and Payment_Check = 'N' or Response_Code IS NULL
Expected Result if the payment_check is N or response_code is null
Payment_Generated = 'Y',
Transaction_ID = '123',
Response_Msg = 'ok',
Response_Code = '1',
Created_on = '2020-05-29T11:29:30'
Order_ID = '10'
Payment_Check = 'N'

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.

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
) ;

Oracle - SubQuery returning Multiple rows

I have the following table structure:
HSM
HSM_EXC_CODE Y VARCHAR2(60)
HSM_INSTR_CODE Y VARCHAR2(60)
HSM_ISIN Y VARCHAR2(60)
HSM_VWD_TICKERSYMBL Y VARCHAR2(80)
TENFORE_EXCHANGE_MAP
HS_MARKET Y VARCHAR2(40)
TF_EXCHANGE Y VARCHAR2(40)
TFV
TFE_ID Y NUMBER(22)
TFE_VSE_CODE Y VARCHAR2(1000)
Different TFE_ID can have same TFE_VSE_CODE! I think this is what I'm missing in the update query below.
VSD
VSD_ON Y VARCHAR2(160)
VSD_ISIN Y VARCHAR2(15)
The tables are connected like the following:
TENFORE_EXCHANGE_MAP.HS_MARKET = HSM.HSM_EXC_CODE
TENFORE_EXCHANGE_MAP.TF_EXCHANGE = TFV.TFE_ID
I'm trying to fill hsm_isin and hsm_on fields. To reach the goal I'm trying to generate the names from hsm.hsm_exc_code . tfv.tfe_vse_code. But I'm doing it wrong, cause I'm getting the error from the topic. This is what I have tried:
UPDATE hsm
SET hsm_isin =
(SELECT distinct vsd.vsd_isin
FROM vsd, tfv, TENFORE_EXCHANGE_MAP
WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id)
,hsm.hsm_vwd_tickersymbl =
(SELECT distinct vsd.vsd_on
FROM vsd, tfv, TENFORE_EXCHANGE_MAP
WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id);
There must be more than one line for key in either first or second subquery:
Try something like:
SELECT hsm.hsm_instr_code,
count( distinct( vsd.vsd_on ) ) cnt1,
count( distinct( vsd.vsd_isin ) ) cnt2
FROM vsd, tfv, TENFORE_EXCHANGE_MAP, hsm
WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id
GROUP BY hsm.hsm_instr_code
HAVING count( distinct( vsd.vsd_on ) ) > 1 OR count( distinct( vsd.vsd_isin ) ) > 1
NOTE: Once you fix multiline problem, you can simplify two subqueries in one, like below:
UPDATE hsm SET ( hsm_isin, hsm.hsm_vwd_tickersymbl ) =
(SELECT distinct vsd.vsd_isin, vsd.vsd_on
FROM vsd, tfv, TENFORE_EXCHANGE_MAP
WHERE vsd.vsd_on = hsm.hsm_instr_code || '.' || tfv.tfe_vse_code
AND hsm.hsm_exc_code = TENFORE_EXCHANGE_MAP.HS_MARKET
AND TENFORE_EXCHANGE_MAP.TF_EXCHANGE = tfv.tfe_id);