ORA-00907: missing right parenthesis - oracle

SELECT DISTINCT( EMP.EMPLOYEEID ),
EMP.EMPLOYEECODE,
EMP.EMPLOYEENAME,
EMP.HOMEADDRESS,
DESIG.DESIGNATIONNAME
FROM HRM_EMPLOYEE EMP,
COM_DESIGNATION DESIG,
COM_DEPARTMENT DEPT,
COM_COMPANY COMP,
HRM_EMPLOYEEDEPARTMENTS EMPDEPT,
USR_USERS USRS
WHERE EMP.EMPLOYEEID = EMPDEPT.EMPLOYEEID AND
EMP.DESIGNATIONID = DESIG.DESIGNATIONID AND
DESIG.DEPARTMENTID = EMPDEPT.DEPARTMENTID AND
EMP.STATUS IN (SELECT STAT STAT
FROM
(
CASE
When (:status = 0) THEN
SELECT 1 STAT FROM dual
UNION ALL
SELECT 2 STAT FROM dual
else
Select :status STAT from dual
end
)
xx
)
Actually my need is: stow the records according to the parameter passing. if tat parameter i wish to show all records.

You cannot have a CASE statement as a table expression (unless perhaps if using nested table types). But why so complicated? Instead of this:
EMP.STATUS IN (SELECT STAT STAT
FROM
(
CASE
When (:status = 0) THEN
SELECT 1 STAT FROM dual
UNION ALL
SELECT 2 STAT FROM dual
else
Select :status STAT from dual
end
)
xx
)
Write this:
(EMP.STATUS IN (1, 2) AND :status = 0) OR
(EMP.STATUS = :status)

SELECT DISTINCT( EMP.EMPLOYEEID ),
EMP.EMPLOYEECODE,
EMP.EMPLOYEENAME,
EMP.HOMEADDRESS,
DESIG.DESIGNATIONNAME
FROM HRM_EMPLOYEE EMP,
COM_DESIGNATION DESIG,
COM_DEPARTMENT DEPT,
COM_COMPANY COMP,
HRM_EMPLOYEEDEPARTMENTS EMPDEPT,
USR_USERS USRS
WHERE EMP.EMPLOYEEID = EMPDEPT.EMPLOYEEID AND
EMP.DESIGNATIONID = DESIG.DESIGNATIONID AND
DESIG.DEPARTMENTID = EMPDEPT.DEPARTMENTID AND
(
(EMP.STATUS IN (1, 2) and :status = 0)
or :status <> 0 --This will not filter your status, as I expect you want it to do so
)

Related

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?

Correlated SQL Query using With Clause

I am trying to modify below query :
SELECT START_END.*, START_END.LOAD_TIME_END - START_END.LOAD_TIME_START
FROM
(
SELECT START.JOB_ID, START.LOAD_TIME_START, END.LOAD_TIME_END
FROM
(
SELECT JOB_ID,LOAD_TIME AS LOAD_TIME_START
FROM JOB_CTRL_RECON JCR
INNER JOIN
(
SELECT JOB_ID AS JOB_ID_S, MAX(RECON_ID) AS S_MAX_RECON_ID
FROM job_ctrl_recon
where job_id in ('1','2')
and count_type = 'Source'
GROUP BY JOB_ID
) SMAX
ON JCR.JOB_ID = SMAX.S_MAX_RECON_ID
AND JCR.RECON_ID = SMAX.RECON_ID
) START
INNER JOIN
(
SELECT JOB_ID,LOAD_TIME AS LOAD_TIME_END FROM JOB_CTRL_RECON JCR INNER JOIN
(
SELECT JOB_ID AS JOB_ID_T, MAX(RECON_ID) AS T_MAX_RECON_ID
FROM job_ctrl_recon
where job_id in ('1','2')
and count_type = 'Target'
GROUP BY JOB_ID
) TMAX
ON JCR.JOB_ID = TMAX.T_MAX_RECON_ID
AND JCR.RECON_ID = TMAX.RECON_ID
) END
ON START.JOB_ID = END.JOB_ID
) START_END
to a query using with clause as below :
How can i eliminate the multiple use of where job_id in ('1','2') using with clause something like below :
WITH A AS
(
SELECT JOB_ID FROM JOB_CTRL_RECON WHERE JOB_ID IN ('60','67')
)
SELECT START_END.*, START_END.LOAD_TIME_END - START_END.LOAD_TIME_START
FROM
(
SELECT START.JOB_ID, START.LOAD_TIME_START, END.LOAD_TIME_END
FROM
(
SELECT JOB_ID,LOAD_TIME AS LOAD_TIME_START
FROM JOB_CTRL_RECON JCR
INNER JOIN
(
SELECT JOB_ID AS JOB_ID_S, MAX(RECON_ID) AS S_MAX_RECON_ID
FROM job_ctrl_recon
where job_id in (????)
and count_type = 'Source'
GROUP BY JOB_ID
) SMAX
ON JCR.JOB_ID = SMAX.S_MAX_RECON_ID
AND JCR.RECON_ID = SMAX.RECON_ID
) START
INNER JOIN
(
SELECT JOB_ID,LOAD_TIME AS LOAD_TIME_END
FROM JOB_CTRL_RECON JCR
INNER JOIN
(
SELECT JOB_ID AS JOB_ID_T, MAX(RECON_ID) AS T_MAX_RECON_ID
FROM job_ctrl_recon
where job_id in (????)
and count_type = 'Target'
GROUP BY JOB_ID
) TMAX
ON JCR.JOB_ID = TMAX.T_MAX_RECON_ID
AND JCR.RECON_ID = TMAX.RECON_ID
) END
ON START.JOB_ID = END.JOB_ID
) START_END, A
WHERE START_END.JOB_ID = A.JOB_ID
Instead of WITH, you may be able to simplify the code using a KEEP with an analytic function. The code is a bit weird at first, but it lets you only specify the JOB_ID once and it only has to read from the table once.
--Load time start and end for each JOB_ID, based on the latest RECON_ID.
select
job_id,
max(load_time_start) load_time_start,
max(load_time_end) load_time_end,
max(load_time_end) - max(load_time_start) diff
from
(
--First and last load time for each set of rows, with some debug data.
select job_id, count_type, recon_id, load_time
,last_value(case when count_type = 'Source' then load_time else null end)
over (
partition by job_id, count_type
order by recon_id
rows between unbounded preceding and unbounded following
) load_time_start
,last_value(case when count_type = 'Target' then load_time else null end)
over (
partition by job_id, count_type
order by recon_id
rows between unbounded preceding and unbounded following
) load_time_end
from job_ctrl_recon
where job_id in (1,2)
)
group by job_id
order by job_id;
For this table and data:
create table job_ctrl_recon(job_id number, recon_id number, count_type varchar2(100), load_time date);
alter session set nls_date_format = 'DD-Mon-RR';
insert into job_ctrl_recon
select 1, 400, 'Source', '24-Feb-18' from dual union all
select 1, 40, 'Source', '23-Feb-18' from dual union all
select 1, 89, 'Target', '25-Feb-18' from dual union all
select 1, 8, 'Target', '25-Feb-18' from dual union all
select 2, 700, 'Source', '24-Feb-18' from dual union all
select 2, 8, 'Source', '23-Feb-18' from dual union all
select 2, 567, 'Target', '25-Feb-18' from dual union all
select 2, 45, 'Target', '25-Feb-18' from dual union all
select 2, 678, 'Target', '25-Feb-18' from dual;
commit;
These are the results:
JOB_ID LOAD_TIME_START LOAD_TIME_END DIFF
------ --------------- ------------- ----
1 2018-02-24 2018-02-25 1
2 2018-02-24 2018-02-25 1
Try this one. Hope it helps. Thanks.
old: where job_id in (????)
new: where job_id in (select JOB_ID from A)

oracle select records today but exclude those which may exist in the previous days

two records should exist in a table in oracle, one is from the request and the other is from the response.
i would like to select all those records from today but the problem is the other pair might be existing from the previous two days or more.
how do i ensure that the records that will be returned are only 1 and will not exist in the previous days
select A.referenceNum, A.datetime, A.Type from table A where A.datetime >= sysdate - 1
To visualize, i would like to select only reference num 789ef.
ReferenceNum DateTime Type
123ab (datetoday) Request
123ab (datetoday) Response
456cd (datetoday) Request
456cd (datetoday-2) Response
789ef (datetoday) Request
You can do it in a single table scan (compared to using NOT EXISTS which will use two table scans) using analytic functions:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name ( ReferenceNum, DateTime, Type ) AS
SELECT '123ab', SYSDATE, 'Request' FROM DUAL UNION ALL
SELECT '123ab', SYSDATE, 'Response' FROM DUAL UNION ALL
SELECT '456cd', SYSDATE, 'Request' FROM DUAL UNION ALL
SELECT '456cd', SYSDATE - 2, 'Response' FROM DUAL UNION ALL
SELECT '789ef', SYSDATE, 'Request' FROM DUAL;
Query 1:
SELECT ReferenceNum
FROM (
SELECT ReferenceNum,
COUNT( CASE WHEN TRUNC( DateTime ) = TRUNC( SYSDATE ) THEN 1 END )
OVER ( PARTITION BY ReferenceNum ) AS num_today,
COUNT( CASE WHEN TRUNC( DateTime ) <> TRUNC( SYSDATE ) THEN 1 END )
OVER ( PARTITION BY ReferenceNum ) AS num_other_day
FROM table_name t
)
WHERE num_today = 1
AND num_other_day = 0
Results:
| REFERENCENUM |
|--------------|
| 789ef |
Use NOT EXISTS operator
SELECT A.referenceNum, A.datetime, A.Type
from table A
where A.datetime >= sysdate - 1
AND NOT EXISTS (
SELECT null FROM table B
WHERE A.referenceNum = B.referenceNum
AND b.datetime < a.datetime
)
SELECT referenceNum
FROM A A1
WHERE TRUNC(A1.datetime) > TRUNC(sysdate)-1
AND A1.Type = 'Request'
AND NOT EXISTS
(SELECT 1
FROM A
WHERE TRUNC(A.datetime) >= TRUNC(sysdate)-1
AND A.Type = 'Response'
AND A.referenceNum = A1.referenceNum
)

Result set different from query than from stored procedure in Oracle

I have a sp that is giving me wrong results. That sp returns a cursor which is missing some rows. Now
When we fire the query within the sp directly in a query window, correct results come up. The only difference in this query is that instead of a cursor, the query directly outputs the result set; i.e. no cursor is opened when we fire the query directly.
When we recompile the sp, correct results come up for some time, and then it is back to missing rows after some time.
Have not been able to make much progress on this. Any pointers on what to look for, how to make progress etc. are much appreciated. I can paste the sp in here if needed, and probably the sp can be rewritten in some other way to make the problem go away, but I am more interested in knowing root cause - what could be causing this strange problem?
SP
CREATE OR REPLACE PROCEDURE "SP_GETTAXSETTINGCHANNELINFO" (v_bid varchar2,
p_rdTaxSetting out SYS_REFCURSOR) as
vT_bid varchar2(100);
begin
vT_bid := v_bid;
Open p_rdTaxSetting for
Select taxmappingid, CHANNELNAME,PROPERTYNAME, PROPERTYID, TAXTYPE, ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE
FROM (
WITH ChannelData AS (
SELECT ONDEMAND_SELECTED_SOURCES AS OnDemandChannels,
SCHEDULED_SELECTED_SOURCES AS ScheduledChannels
FROM SUB_ORDER S
INNER JOIN CONTACT C ON S.Supplier_ID = C.Contact_ID
WHERE C.BID = vT_bid
AND S.STATUS='Complete' and current_ind = 1
),
Property_Data As (
SELECT
SC.PROPERTY_NUM AS PropertyID
,SC.Company_Name as PropertyName
,SC.competitor_number as PropertyOrder
FROM SUB_ORDER S
INNER JOIN CONTACT C ON S.Supplier_ID = C.Contact_ID
INNER JOIN SUB_ORDER_COMPETITOR SC ON S.Order_Id = SC.Order_ID
WHERE C.BID = vT_bid
AND S.STATUS='Complete' and current_ind = 1
),
ODChannel as (
select regexp_substr (OnDemandChannels, '[^,]+', 1, rn) as Channel
from (Select OnDemandChannels From ChannelData)
cross join
(select rownum rn
from (select max (length (regexp_replace (OnDemandChannels, '[^,]+'))) + 1 max_value
from (Select OnDemandChannels From ChannelData))
connect by level <= max_value)
where regexp_substr (OnDemandChannels, '[^,]+', 1, rn) is not null
),
SCDChannel As (
select regexp_substr (ScheduledChannels, '[^,]+', 1, rn) as Channel
from (Select ScheduledChannels From ChannelData)
cross join
(select rownum rn
from (select max (length (regexp_replace (ScheduledChannels, '[^,]+'))) + 1 max_value
from (Select ScheduledChannels From ChannelData))
connect by level <= max_value)
where regexp_substr (ScheduledChannels, '[^,]+', 1, rn) is not null
),
ChaData1 As (
Select
CASE lower(Channel)
WHEN 'galileo' then 'GDS'
else Channel
end AS Channel
from ODChannel
),
ChaData2 As (
Select CASE lower(Channel)
WHEN 'galileo' then 'GDS'
else Channel
end AS Channel FROM SCDChannel
),
PropData As (
Select Distinct PropertyID,PropertyName from Property_Data
order by PropertyID
),
AllChannel AS (
Select Channel From ChaData1
union
Select Channel From ChaData2
),
Prop_Cha_Data As (
Select Distinct Channel,PropertyID,PropertyName from AllChannel,Property_Data
order by Channel,PropertyID
),
Tax_Channel_Prop_Exists AS ( select
CASE lower(CHANNELNAME)
WHEN 'galileo' then 'GDS'
else CHANNELNAME
end AS CHANNELNAME,
PropertyName As PROPERTYNAME, TCP.PROPERTYID As PROPERTYID,TAXTYPE,
ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE,taxmappingid
from TAXSETTING_CHANNEL_PROPERTY TCP
INNER JOIN AllChannel PC On TRIM (CASE lower(TCP.CHANNELNAME)
WHEN 'galileo' then 'GDS'else TCP.CHANNELNAME end) = TRIM(PC.Channel)
INNER JOIN PropData CP On TCP.PROPERTYID = CP.PropertyID
where TCP.BID=vT_bid
)
Select Distinct taxmappingid, CHANNELNAME,PROPERTYNAME, PROPERTYID, TAXTYPE, ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE from Tax_Channel_Prop_Exists
UNION ALL
select DISTINCT 0 As taxmappingid, PCD.Channel As CHANNELNAME,
PCD.PropertyName As PROPERTYNAME ,
PCD.propertyid As PROPERTYID,
-1 As TAXTYPE,
0 As ISPARTIALLYINCLUSIVE,
'' As PARTIALLYINCLUSIVEVALUE
FROM Prop_Cha_Data PCD
WHERE NOT EXISTS (
Select taxmappingid FROM Tax_Channel_Prop_Exists E
WHERE E.channelname = PCD.Channel AND
E.propertyid = PCD.propertyid )
)
Order by PROPERTYNAME,CHANNELNAME ;
end SP_GetTaxSettingChannelInfo;

Can one not reference a CTE twice in a query in Oracle?

I have a chain of CTEs, and at the end I want to select from the last one twice. Oracle lets me do either of the selects but not a union of both of them (even if I do "select * from (select union select)". The only thing I could narrow down the problem to was referring to "runinfo" in two selects, but that in and of itself is not the problem as Shannon shows.
WITH lastTen AS (
SELECT id
FROM (SELECT autobuild_id, id, rank() OVER (PARTITION BY autobuild_id ORDER BY id DESC) as rank
FROM runs
WHERE status='FINISHED' AND type='FULL' AND build_failed in ('n', 'N'))
WHERE rank <= 10
),
recentAvg AS (
SELECT autobuild_id, avg(elapsed) avgtime
FROM runs
JOIN lastTen ON (runs.id = lastTen.id)
GROUP BY autobuild_id
),
runinfo AS (
SELECT autobuildid, runid, changelist, status, age
FROM (
SELECT runs.autobuild_id autobuildid, runs.id runid, changelist, runs.status status, runs.create_date, a.avgtime,
CASE WHEN status = 'RUNNING' THEN TO_NUMBER(sysdate - start_date)*86400 -- in seconds to compare to elapsed
WHEN status = 'TO BE' THEN TO_NUMBER(sysdate - create_date) -- in days
END AS age
FROM runs
LEFT JOIN recentAvg a ON (runs.autobuild_id = a.autobuild_id)
)
WHERE (status = 'RUNNING' AND age > avgtime * 1.5)
OR (status = 'TO BE' AND age > 1)
ORDER BY autobuildid, runid DESC
),
running AS (
SELECT autobuilds.id, name, runid, changelist, runinfo.status, age
FROM runinfo
JOIN autobuilds ON (runinfo.autobuildId=autobuilds.id)
WHERE runinfo.status='RUNNING'
),
tobe AS (
SELECT autobuildid, name, runid, changelist, status, age
FROM (SELECT autobuildid, name, runid, changelist, runinfo.status, age, RANK() OVER (PARTITION BY autobuildid ORDER BY runid DESC) AS rank
FROM runinfo
JOIN autobuilds ON (runinfo.autobuildid=autobuilds.id)
WHERE runinfo.status='TO BE')
WHERE rank=1
)
SELECT * FROM running
UNION ALL
SELECT * FROM tobe
What is the simpliest query that doesn't work for you?
What is the complete error message?
Oracle does allow a CTE to be referenced twice, but I am not understanding what you are attempting:
SQL> with a as (select * from dual)
2 , b as (select * from a)
3 , c as (select * from b)
4 select *
5 from b, c
6 where b.dummy = c.dummy;
D D
- -
X X

Resources