Add row num not working in oracle - oracle

I want to add rownum in my below oracle query but it is giving me error as
ORA-30484: missing window specification for this function
Here is my query
SELECT ROW_NUMBER () AS sr_no, pn.lease_num, hz.party_name,
flt.location_code, flt.office flat_no, NULL action, la.no_of_days,
NULL remarks, flt.location_id flat_id, pn.lease_id
FROM xxcus.xxacl_pn_leases_all la,
pn_leases_all pn,
(SELECT *
FROM pn_locations_all flat
WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) bld,
(SELECT *
FROM pn_locations_all flat
WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) flr,
(SELECT *
FROM pn_locations_all flat
WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) flt,
pn_properties_all prop,
hz_parties hz,
apps.hz_cust_accounts sc1
WHERE la.lease_id = pn.lease_id
AND pn.location_id = flt.location_id
AND flt.parent_location_id = flr.location_id
AND flr.parent_location_id = bld.location_id
AND bld.property_id = prop.property_id
AND pn.customer_id = sc1.cust_account_id
AND sc1.party_id = hz.party_id
AND la.type_of_booking = 50
AND prop.property_id = '1'
AND bld.location_id = '1309'
kindly help what is wrong
I am using ORACLE

See documentation for ROW_NUMBER. You need to write something like:
SELECT ROW_NUMBER() OVER (PARTITION BY la.type_of_booking ORDER BY la.lease_id)
FROM xxcus.xxacl_pn_leases_all la
or
SELECT ROW_NUMBER() OVER (ORDER BY la.lease_id)
FROM xxcus.xxacl_pn_leases_all la

Related

Pl / SQL Oracle helps to run a Date in Subquery

How could I get the date of the Maximum Value, by means of a subquery
I can't put the Date in the Main query because I would have to add it to the group by it would bring me a lot of data
Here is the Code:
SELECT MAX (A1.VALOR) AS VALOR,
(SELECT sq1.FECHA
FROM VARIABLE_VALORES_SMEC sq1
WHERE sq1.ID_AGENTE = A1.ID_AGENTE)
MES, -- {<-- Here is the Problem}
(SELECT CODIGO_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Agentess,
(SELECT NOMBRE_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Nombre_Agente
FROM VARIABLE_VALORES_SMEC A1
WHERE A1.VALOR < '1'
AND A1.VALOR != '0'
AND A1.ID_AGENTE IN (SELECT C1.ID_AGENTE
FROM VARIABLE_VALORES_SMEC C1
WHERE A1.FECHA = C1.FECHA)
AND A1.ID_AGENTE IN (SELECT B1.ID_AGENTE
FROM AGENTES B1
WHERE ID_CATEGORIA_AGENTE = 'AC006')
AND (A1.FECHA BETWEEN (ADD_MONTHS (TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))
AND (LAST_DAY (
ADD_MONTHS (
TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))))
AND A1.ID_VARIABLE LIKE '%_calc_total_pot#%'
GROUP BY ID_AGENTE
Am I correct that you need (fecha) for maximum A1.VALOR?
If - yes, you can use the following query, or if - no, just replace A1.VALOR with the required column in keep() clause:
SELECT MAX (A1.VALOR) AS VALOR,
max(A1.FECHA)keep(dense_rank first order by A1.VALOR desc) MES, -- A1.VALOR is used here as sort key, replace it with what you want
(SELECT CODIGO_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Agentess,
(SELECT NOMBRE_AGENTE
FROM AGENTES
WHERE ID_AGENTE = A1.ID_AGENTE)
Nombre_Agente
FROM VARIABLE_VALORES_SMEC A1
WHERE A1.VALOR < '1'
AND A1.VALOR != '0'
AND A1.ID_AGENTE IN (SELECT C1.ID_AGENTE
FROM VARIABLE_VALORES_SMEC C1
WHERE A1.FECHA = C1.FECHA)
AND A1.ID_AGENTE IN (SELECT B1.ID_AGENTE
FROM AGENTES B1
WHERE ID_CATEGORIA_AGENTE = 'AC006')
AND (A1.FECHA BETWEEN (ADD_MONTHS (TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))
AND (LAST_DAY (
ADD_MONTHS (
TO_DATE ( :FECHAIN, 'MM/DD/YYYY'),
-1))))
AND A1.ID_VARIABLE LIKE '%_calc_total_pot#%'
GROUP BY ID_AGENTE
You can use row_number analytical function to fetch one record for which value is highest and use the fecha of that record. Use following sub query:
(Select fecha from
(SELECT sq1.FECHA, row_number() over (order by sq1.value desc nulls last) as rn
FROM VARIABLE_VALORES_SMEC sq1
WHERE sq1.ID_AGENTE = A1.ID_AGENTE)
Where rn = 1) MES

Conditional data selection in oracle

I have a set of data by using query and in this data I need to select some data according to following condition
If SIGNSTAT = 4 then select all row which have SIGNSTAT = 4
Otherwise select latest create date row data.
Please help to found the data
My query is as
WITH CTE AS(SELECT C2C.NO AS CONTRACTNO,
C.BRANCH,
C.IDCLIENT,
--C.PAN,
C.NAMEONCARD,
C.CREATEDATE,
C.SIGNSTAT,
ROW_NUMBER ()
OVER (PARTITION BY C2C.NO ORDER BY C.CREATEDATE DESC)
AS ROW_NUM
FROM A4M.TCONTRACTCARDITEM C2C, A4M.TCARD C,A4M.TREFERENCECARDPRODUCT RCP
WHERE 1 = 1 AND C.BRANCH = C2C.BRANCH AND C.PAN = C2C.PAN AND C2C.NO = '700000075333'
AND C.BRANCH = RCP.BRANCH AND C.CARDPRODUCT = RCP.CODE
AND UPPER(RCP.NAME) LIKE '%SUPPL%'
)
SELECT * FROM CTE
--WHERE ROW_NUM = 1
Result set:
CONTRACTNO BRANCH IDCLIENT NAMEONCARD CREATEDATE SIGNSTAT ROW_NUM
700000075333 1 1215995 SAMIR CHANDRA DHAR 14-Jul-19 4 2
700000075333 1 1215995 SAMIR CHANDRA DHAR 20-Aug-19 3 1
Is this what you need?
WITH data AS(SELECT C2C.NO AS CONTRACTNO,
C.BRANCH,
C.IDCLIENT,
--C.PAN,
C.NAMEONCARD,
C.CREATEDATE,
C.SIGNSTAT,
ROW_NUMBER ()
OVER (PARTITION BY C2C.NO ORDER BY C.CREATEDATE DESC)
AS ROW_NUM
FROM A4M.TCONTRACTCARDITEM C2C, A4M.TCARD C,A4M.TREFERENCECARDPRODUCT RCP
WHERE 1 = 1 AND C.BRANCH = C2C.BRANCH AND C.PAN = C2C.PAN AND C2C.NO = '700000075333'
AND C.BRANCH = RCP.BRANCH AND C.CARDPRODUCT = RCP.CODE
AND UPPER(RCP.NAME) LIKE '%SUPPL%'
), cnt AS (SELECT COUNT(1) total
FROM data
WHERE SIGNSTAT=4)
SELECT *
FROM data
WHERE SIGNSTAT=4
UNION ALL
SELECT *
FROM data
JOIN cnt
ON cnt.total=0
WHERE data.ROW_NUM = 1
I got the desire result by following query
WITH data AS(SELECT C2C.NO AS CONTRACTNO,
C.BRANCH,
C.IDCLIENT,
C.PAN,
C.NAMEONCARD,
C.CREATEDATE,
C.SIGNSTAT,
ROW_NUMBER ()
OVER (PARTITION BY C2C.NO ORDER BY C.CREATEDATE DESC)
AS ROW_NUM
FROM A4M.TCONTRACTCARDITEM C2C, A4M.TCARD C,A4M.TREFERENCECARDPRODUCT RCP
WHERE 1 = 1 AND C.BRANCH = C2C.BRANCH AND C.PAN = C2C.PAN AND C2C.NO = '700000075333'
AND C.BRANCH = RCP.BRANCH AND C.CARDPRODUCT = RCP.CODE
AND UPPER(RCP.NAME) LIKE '%SUPPL%') SELECT CONTRACTNO,BRANCH,IDCLIENT FROM data WHERE SIGNSTAT=4 UNION ALL (SELECT CONTRACTNO,BRANCH,IDCLIENT FROM data WHERE data.ROW_NUM = 1 MINUS SELECT CONTRACTNO,BRANCH,IDCLIENT FROM data WHERE SIGNSTAT=4)
You can achieve this with minor change in your original query. Use CASE WHEN as following:
WITH CTE AS(SELECT C2C.NO AS CONTRACTNO,
C.BRANCH,
C.IDCLIENT,
--C.PAN,
C.NAMEONCARD,
C.CREATEDATE,
C.SIGNSTAT,
CASE WHEN C.SIGNSTAT = 4 THEN 1 ELSE ROW_NUMBER ()
OVER (PARTITION BY C2C.NO ORDER BY C.CREATEDATE DESC) END
AS ROW_NUM
FROM A4M.TCONTRACTCARDITEM C2C, A4M.TCARD C,A4M.TREFERENCECARDPRODUCT RCP
WHERE 1 = 1 AND C.BRANCH = C2C.BRANCH AND C.PAN = C2C.PAN AND C2C.NO = '700000075333'
AND C.BRANCH = RCP.BRANCH AND C.CARDPRODUCT = RCP.CODE
AND UPPER(RCP.NAME) LIKE '%SUPPL%'
)
SELECT * FROM CTE
WHERE ROW_NUM = 1
Cheers!!

procedure failurecompilation

I have an existing procedure which creates a table adwstg.switchhold_notificatn_stg
Suddenly it is throwing error in creating the table. I didn't change anything the statement.
First I thought the error is with ''No'' . So find & replace '' with '
But it is throwing error at line:
nvl(ba300_z51.sent_650_01, 'No') sent_650_01,
error(27,29): PLS-00103: Encountered the symbol "NO" when expecting one of the following: * & = - + ; < / > at in is mod remainder not rem <> or != or ~= >= <= <> and or like like2 like4 likec between || multiset member submultiset***
Below is the query :
create table adwstg.switchhold_notificatn_stg nologging parallel (degree 8) compress as
select distinct
bad3700.createdon,
bad3700.uc_pod_ext,
bpc.cacont_acc,
bad3700.notificatn,
bad3700.nfcat_code,
nvl(ba300_z51.sent_650_01, ''No'') sent_650_01,
decode(ba300_z51.sent_650_01,''No'',''--'',''Yes'',to_char(ba300_z51.ucswmsgdat,''yyyymmdd''),''--'') date_sent_650_01,
nvl(to_char(ba300_z51.ucswmsgtim,''hh24:mi:ss''),''--'') time_sent_650_01,
nvl(ba300_z52.received_650_02, ''No'') received_650_02,
decode(ba300_z52.received_650_02,''No'',''--'',''Yes'',to_char(ba300_z52.ucswmsgdat,''yyyymmdd''),''--'') date_received_650_02,
nvl(to_char(ba300_z52.ucswmsgtim,''hh24:mi:ss''),''--'') time_received_650_02,
nvl(ba300_z20.received_814_20, ''No'') received_814_20,
decode(ba300_z20.received_814_20,''No'',''--'',''Yes'',to_char(ba300_z20.ucswmsgdat,''yyyymmdd''),''--'') date_received_814_20,
nvl(to_char(ba300_z20.ucswmsgtim,''hh24:mi:ss''),''--'') time_received_814_20,
case
when trim(bad3700.nfcat_code) = ''SH01'' and zet.ext_ui is not null then ''ADDED''
when trim(bad3700.nfcat_code) = ''SH01'' and zet.ext_ui is null then ''NOT PROCESSED''
when trim(bad3700.nfcat_code) in (''SH02'',''SH03'') and zet.ext_ui is null then ''REMOVED''
when trim(bad3700.nfcat_code) in (''SH02'',''SH03'') and zet.ext_ui is not null then ''NOT PROCESSED''
else ''NOT PROCESSED''
end work_order_check
from
(select distinct *
from
(select
trunc(createdon) createdon,
trim(notificatn) notificatn,
trim(uc_pod_ext) uc_pod_ext,
trim(not_type) not_type,
trim(nfcat_code) nfcat_code,
row_number () over (partition by trim(uc_pod_ext),trunc(createdon) order by trim(notificatn) desc) rnum
from birpt.bic_azfc_ds3700
where upper(trim(not_type)) = ''SH''
and trim(bic_zdiscstat) = ''E0010'')
where rnum = 1
) bad3700
left outer join
(
select distinct ucinstalla, uc_pod_ext, datefrom, dateto
from birpt.bi0_qucinstalla
where objvers = ''A''
) bqi
on (trim(bad3700.uc_pod_ext) = trim(bqi.uc_pod_ext)
and trunc(bad3700.createdon) between bqi.datefrom and bqi.dateto)
left outer join
(
select distinct cacont_acc, ucinstalla, ucmovein_d, ucmoveoutd
from birpt.bi0_puccontract
where objvers = ''A''
) bpc
on (trim(bqi.ucinstalla) = trim(bpc.ucinstalla)
and trunc(bad3700.createdon) between bpc.ucmovein_d and bpc.ucmoveoutd)
left outer join
(select distinct *
from
(select
trim(ucswtpodex) ucswtpodex,
trunc(ucswmsgdat) ucswmsgdat,
ucswmsgtim,
case --650_01 CHECK
when trim(uc_mdcat) = ''Z51'' then ''Yes''
else ''No''
end sent_650_01,
row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum
from birpt.bic_azudeds0300
where trim(uc_mdcat) = ''Z51'')
where rnum = 1) ba300_z51
on (trim(bad3700.uc_pod_ext) = trim(ba300_z51.ucswtpodex)
and trunc(bad3700.createdon)= trunc(ba300_z51.ucswmsgdat))
left outer join
(select distinct *
from
(select
trim(ucswtpodex) ucswtpodex,
trunc(ucswmsgdat) ucswmsgdat,
ucswmsgtim,
case --650_02 CHECK
when trim(uc_mdcat) = ''Z52'' then ''Yes''
else ''No''
end received_650_02,
row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum
from birpt.bic_azudeds0300
where trim(uc_mdcat) = ''Z52'')
where rnum = 1) ba300_z52
on (trim(bad3700.uc_pod_ext) = trim(ba300_z52.ucswtpodex)
and trunc(bad3700.createdon)= trunc(ba300_z52.ucswmsgdat))
left outer join
(select distinct *
from
(select
trim(ucswtpodex) ucswtpodex,
trunc(ucswmsgdat) ucswmsgdat,
ucswmsgtim,
case --814_20 CHECK
when trim(uc_mdcat) = ''Z20'' then ''Yes''
else ''No''
end received_814_20,
row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum
from birpt.bic_azudeds0300
where trim(uc_mdcat) = ''Z20'')
where rnum = 1) ba300_z20
on (trim(bad3700.uc_pod_ext) = trim(ba300_z20.ucswtpodex)
and trunc(bad3700.createdon)= trunc(ba300_z20.ucswmsgdat))
left outer join
(select distinct ext_ui
from isurpt.zcs_esiid_tamper
) zet
on (trim(bad3700.uc_pod_ext) = trim(zet.ext_ui));
If you remove all doubled single quotes (perform replace function in any text editor), your code will be valid (as far as syntax is concerned).
I don't have your tables to test it, and there are too many of them with too many columns to create a test case by myself.
The question is: why did you double them in the first place?

Oracle Query error with debugged query

While debugging the query, I got error as
ORA-00923: FROM keyword not found where expected
Below is my debugged query
SELECT pn.lease_num,
hz.party_name,
flt.location_code,
flt.office flat_no,
NULL action,
la.no_of_days,
NULL remarks,
flt.location_id flat_id,
pn.lease_id,
prop.property_id = '1', bld.location_id building_id = '1309'
FROM xxcus.xxacl_pn_leases_all la,
pn_leases_all pn,
(SELECT *
FROM pn_locations_all flat
WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) bld,
(SELECT *
FROM pn_locations_all flat
WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) flr,
(SELECT *
FROM pn_locations_all flat
WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) flt,
pn_properties_all prop,
hz_parties hz,
apps.hz_cust_accounts sc1
WHERE la.lease_id = pn.lease_id
AND pn.location_id = flt.location_id
AND flt.parent_location_id = flr.location_id
AND flr.parent_location_id = bld.location_id
AND bld.property_id = prop.property_id
AND pn.customer_id = sc1.cust_account_id
AND sc1.party_id = hz.party_id
AND la.type_of_booking = 50
I dont know what the error is.
I am using ORACLE
This part is wrong:
prop.property_id = '1', bld.location_id building_id = '1309'
It seems that you need to remove it and add some WHERE conditions:
SELECT pn.lease_num,
hz.party_name,
flt.location_code,
flt.office flat_no,
NULL action,
la.no_of_days,
NULL remarks,
flt.location_id flat_id,
pn.lease_id
FROM xxcus.xxacl_pn_leases_all la,
pn_leases_all pn,
(SELECT *
FROM pn_locations_all flat
WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) bld,
(SELECT *
FROM pn_locations_all flat
WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) flr,
(SELECT *
FROM pn_locations_all flat
WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) flt,
pn_properties_all prop,
hz_parties hz,
apps.hz_cust_accounts sc1
WHERE la.lease_id = pn.lease_id
AND pn.location_id = flt.location_id
AND flt.parent_location_id = flr.location_id
AND flr.parent_location_id = bld.location_id
AND bld.property_id = prop.property_id
AND pn.customer_id = sc1.cust_account_id
AND sc1.party_id = hz.party_id
AND la.type_of_booking = 50
AND prop.property_id = '1'
AND bld.location_id = '1309'

Please help me to optimize the below mentioned script as the same table(i.e. Incident_Audit_log) has utilized multiple times?

select A.*
from Incident_Audit_log a where incident_audit_log_id in
(select top 1 incident_audit_log_id from Incident_Audit_log b
where b.incident_id=a.incident_id and b.status_did=a.status_did
and b.tracking_code_did = (select tracking_code_did
from Incident_Audit_log where update_date = (select MAX(update_date)
from Incident_Audit_log where Status_did in (103, 1035)
and incident_id = b.incident_id)
and incident_id = b.incident_id)
order by update_date asc)
I am not sure what you want to achieve but I guess that you want to extract row with new newest update and status_did equal to 13 and 1035.
In that case this should work:
select *
from (
select ROW_NUMBER() OVER(ORDER BY update_date DESC) AS rn,
*
from Incident_Audit_log
where status_did in (103, 1035)
) as SubQueryAlias
where rn = 1
In case not , provide more info.

Resources