Oracle SQL Developer - Error: "FROM keyword not found where expected" - sql

Sorry for my noob question but I'm trying to figure out why my Oracle-SQL indicates the error
"FROM keyword not found where expected" as the picture below:
The code I'm trying to run is the following:
select
PCKCOO AS 'COMPANHIA_DO_PEDIDO_NUMERO_DO_PEDIDO',
PCDOCO AS 'DOCUMENTO_NUMERO_DA_OS_FATURA',
PCDCTO AS 'TIPO_DE_ORDEM',
PCSFXO AS 'SUFIXO_DO_PEDIDO',
rpad(HORDT,'0',6) AS 'HORARIO_DE_LIBERACAO',
FX_PARA_GREGORIANA(HORDJ, 'DD/MM/YYYY')||' '||rpad(HORDT,6,'0') "APROVACAO",
rank() over (partition by pckcoo, pcdoco, pcdcto, pclnid order by FX_PARA_GREGORIANA(HORDJ, 'DD/MM/YYYY')||' '||rpad(HORDT,6,'0') desc) as rank,
FROM PRODDTA.F5543170 a,
proddta.f4209 b,
proddta.f4301 c,
WHERE
PCKCOO = '52171' AND
PCDCTO In ('OP','C1','C2','FZ','OF') AND
((PCTRDJ >= '117060' AND PCTRDJ <= '117090')
or (PCTRDJ >= '116061' AND PCTRDJ <= '116091')
) and
( b.hokcoo = a.pckcoo and
b.hodoco = a.pcdoco and
b.hodcto = a.pcdcto and
B.HOASTS = '2A') and
(c.phkcoo (+)= a.pckcoo and
c.phdoco (+)= COALESCE(TO_NUMBER(REGEXP_SUBSTR(PCOORN, '^(-|+)?d+(.|,)?(d+)?$')), 0) AND
c.phdcto (+)= 'OR')
The code was a lot bigger but I cut it into pieces in order to find out why this is happening (specially regarding the
Line: 3
and
Column: 25
as the error message indicated, which for me makes no sense).
Additionally, when I erase some lines in order to get closer towards the error, the red line (as indicated in the picture) keeps in the first line.
Do you have a guess of why this is happening? (sorry for the basic question again).

You have a trailing comma in the last table (proddta.f4301 c) of FROM clause and should become
...
FROM PRODDTA.F5543170 a,
proddta.f4209 b,
proddta.f4301 c
...
which should be removed.
You also have a trailing comma in your select statement that should also be removed.
...
rank() over (partition by pckcoo, pcdoco, pcdcto, pclnid order by FX_PARA_GREGORIANA(HORDJ, 'DD/MM/YYYY')||' '||rpad(HORDT,6,'0') desc) as rank
...
Finally, for table alias you need to use double instead of single quotes:
select
PCKCOO AS "COMPANHIA_DO_PEDIDO_NUMERO_DO_PEDIDO",
PCDOCO AS "DOCUMENTO_NUMERO_DA_OS_FATURA",
PCDCTO AS "TIPO_DE_ORDEM",
PCSFXO AS "SUFIXO_DO_PEDIDO",
rpad(HORDT,'0',6) AS "HORARIO_DE_LIBERACAO",
...

You have some trailing commas after AS RANK and after proddta.f4301 c.
Also, you can not use single quotes for column aliases, but you need double quotes;
this should work:
SELECT PCKCOO AS "COMPANHIA_DO_PEDIDO_NUMERO_DO_PEDIDO",
PCDOCO AS "DOCUMENTO_NUMERO_DA_OS_FATURA",
PCDCTO AS "TIPO_DE_ORDEM",
PCSFXO AS "SUFIXO_DO_PEDIDO",
RPAD(
HORDT,
'0',
6
)
AS "HORARIO_DE_LIBERACAO",
FX_PARA_GREGORIANA(HORDJ, 'DD/MM/YYYY')
|| ' '
|| RPAD(
HORDT,
6,
'0'
)
"APROVACAO",
RANK()
OVER(
PARTITION BY pckcoo,
pcdoco,
pcdcto,
pclnid
ORDER BY
FX_PARA_GREGORIANA(HORDJ, 'DD/MM/YYYY')
|| ' '
|| RPAD(
HORDT,
6,
'0'
) DESC
)
AS RANK
FROM PRODDTA.F5543170 a,
proddta.f4209 b,
proddta.f4301 c
WHERE PCKCOO = '52171'
AND PCDCTO IN ('OP',
'C1',
'C2',
'FZ',
'OF')
AND ( ( PCTRDJ >= '117060'
AND PCTRDJ <= '117090')
OR ( PCTRDJ >= '116061'
AND PCTRDJ <= '116091'))
AND ( b.hokcoo = a.pckcoo
AND b.hodoco = a.pcdoco
AND b.hodcto = a.pcdcto
AND B.HOASTS = '2A')
AND ( c.phkcoo(+) = a.pckcoo
AND c.phdoco(+) = COALESCE(TO_NUMBER(REGEXP_SUBSTR(PCOORN, '^(-|+)?d+(.|,)?(d+)?$')), 0)
AND c.phdcto(+) = 'OR')
As an aside, you should better switch to ANSI JOIN syntax.

Related

"ORA-00923: FROM keyword not found where expected\n what should I fix

I have an oracle query as follows but when I make changes to pagination the results are different. what should i pass for my code
SELECT *
FROM (
SELECT b.*,
ROWNUM r__
FROM (
select a.KODE_KLAIM,
a.NO_SS,
a.LA,
a.NAMA_TK,
a.KODE_K,
(
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K and rownum=1
) TEM_LAHIR,
(
select TO_CHAR(tk.TLAHIR, 'DD/MM/RRRR')
from KN.VW_KTK tk
where tk.KODE_K = a.KODE_K
and rownum=1
) TLAHIR
from PN.KLAIM a
where nvl(a.STATUS_BATAL,'X') = 'T'
and A.NOMOR IS NOT NULL
and A.TIPE_KLAIM = 'JPN01'
)b
)
where 1 = 1
WHERE ROWNUM < ( ( ? * ? ) + 1 )
WHERE r__ >= ( ( ( ? - 1 ) * ? ) + 1 )
but i run this query i have result ORA-00900: invalid SQL statement
You have three WHERE clauses at the end (and no ORDER BY clause). To make it syntactically valid you could change the second and third WHERE clauses to AND.
However, you mention pagination so what you probably want is to use:
SELECT *
FROM (
SELECT b.*,
ROWNUM r__
FROM (
select ...
from ...
ORDER BY something
)b
WHERE ROWNUM < :page_size * :page_number + 1
)
WHERE r__ >= ( :page_number - 1 ) * :page_size + 1
Note: You can replace the named bind variables with anonymous bind variables if you want.
Or, if you are using Oracle 12 or later then you can use the OFFSET x ROWS FETCH FIRST y ROWS ONLY syntax:
select ...
from ...
ORDER BY something
OFFSET (:page_number - 1) * :page_size ROWS
FETCH FIRST :page_size ROWS ONLY;
Additionally, you have several correlated sub-queries such as:
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K and rownum=1
This will find the first matching row that the SQL engine happens to read from the datafile and is effectively finding a random row. If you want a specific row then you need an ORDER BY clause and you need to filter using ROWNUM AFTER the ORDER BY clause has been applied.
From Oracle 12, the correlated sub-query would be:
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K
ORDER BY something
FETCH FIRST ROW ONLY

How to fix this ORA-00918 error in this SQL code

I have ORA-00918 error with my code and i could not find the problem... the following code gives me this error.
ORA-00918 : column ambiguously defined
can anyone give me some advice? thanks
SELECT * FROM (
SELECT * FROM (
SELECT ROWNUM AS RNUM, A.XML_MSG_ID, A.LOGIN_ID, A.ORIGINATOR, A.RECIPIENT, A.ERROR_CODE, B.DOC_NO, B.DOC_NAME, B.ERROR_MSG
FROM XML_MANAGE_TBL A, XML_REFERENCE_TBL B
WHERE A.XML_MGS_ID = B.XML_MSG_ID
AND A.ERROR_CODE <> '00000000'
AND A.XML_MSG_ID >= '20190528' AND (SUBSTR(A.XML_MSG_ID, 1, 8)) <= '20190604' ) C, EBILL_USER D WHERE D.COMP_NUM = '1258169573' AND C.ORIGINATOR = D.ORIGINATOR )
WHERE RNUM BETWEEN CASE WHEN (1-1) != 0 THEN ((1-1)*50)+1 ELSE (1-1)*50 END
AND 1*50;
The problem is most probably in second subquery select *
SELECT * FROM (
... subquery C ...
) C, EBILL_USER D WHERE ... AND C.ORIGINATOR = D.ORIGINATOR
The table D contains the same columns as the subquery C, for sure the ORIGINATORcolumn
Simple change the second query to SELECT C.* and add only the required columns from D.
The general aproach how to troubleshoot ORA-00918 is to run the query from the innermost subquery and check that the returned column names are unique.
In your case try first, which should be fine
SELECT ROWNUM AS RNUM, A.XML_MSG_ID, A.LOGIN_ID, A.ORIGINATOR, A.RECIPIENT, A.ERROR_CODE, B.DOC_NO, B.DOC_NAME, B.ERROR_MSG
FROM XML_MANAGE_TBL A, XML_REFERENCE_TBL B
WHERE A.XML_MGS_ID = B.XML_MSG_ID
AND A.ERROR_CODE <> '00000000'
AND A.XML_MSG_ID >= '20190528' AND (SUBSTR(A.XML_MSG_ID, 1, 8)) <= '20190604'
Than run the second innermost subquery
SELECT * FROM (
SELECT ROWNUM AS RNUM, A.XML_MSG_ID, A.LOGIN_ID, A.ORIGINATOR, A.RECIPIENT, A.ERROR_CODE, B.DOC_NO, B.DOC_NAME, B.ERROR_MSG
FROM XML_MANAGE_TBL A, XML_REFERENCE_TBL B
WHERE A.XML_MGS_ID = B.XML_MSG_ID
AND A.ERROR_CODE <> '00000000'
AND A.XML_MSG_ID >= '20190528' AND (SUBSTR(A.XML_MSG_ID, 1, 8)) <= '20190604' ) C, EBILL_USER D WHERE D.COMP_NUM = '1258169573' AND C.ORIGINATOR = D.ORIGINATOR
In your IDE (e.g. SQL Developer) you will see one and more columns with a suffix _1 which is a sign of duplicated column that must be excluded (for columns from the equijoin predicate) or renamed.
you just need to remove outermost query and use C.RNUM instead of RNUM in where clause. Try with below code:
SELECT * FROM (
SELECT ROWNUM AS RNUM, A.XML_MSG_ID, A.LOGIN_ID, A.ORIGINATOR, A.RECIPIENT, A.ERROR_CODE, B.DOC_NO, B.DOC_NAME, B.ERROR_MSG
FROM XML_MANAGE_TBL A, XML_REFERENCE_TBL B
WHERE A.XML_MGS_ID = B.XML_MSG_ID
AND A.ERROR_CODE <> '00000000'
AND A.XML_MSG_ID >= '20190528' AND (SUBSTR(A.XML_MSG_ID, 1, 8)) <= '20190604' ) C, EBILL_USER D WHERE D.COMP_NUM = '1258169573' AND C.ORIGINATOR = D.ORIGINATOR
and (C.RNUM BETWEEN CASE WHEN (1-1) != 0 THEN ((1-1)*50)+1 ELSE (1-1)*50 END AND 1*50);

Listagg Overflow function implementation (Oracle SQL)

I am using LISTAGG function for my query, however, it returned an ORA-01489: result of string concatenation is too long error. So I googled that error and found out I can use ON OVERFLOW TRUNCATE and I implemented that into my SQL but now it generates missing right parenthesis error and I can't seem to figure out why?
My query
SELECT DISTINCT cust_id, acct_no, state, language_indicator, billing_system, market_code,
EMAIL_ADDR, DATE_OF_CHANGE, TO_CHAR(DATE_LOADED, 'DD-MM-YYYY') DATE_LOADED,
(SELECT LISTAGG( SUBSTR(mtn, 7, 4),'<br>' ON OVERFLOW TRUNCATE '***' )
WITHIN GROUP (ORDER BY cust_id || acct_no) mtnlist
FROM process.feature WHERE date_loaded BETWEEN TO_DATE('02-08-2018','MM-dd-yyyy')
AND TO_DATE('02-09-2018', 'MM-dd-yyyy') AND cust_id = ffsr.cust_id
AND acct_no = ffsr.acct_no AND filename = 'FEATURE.VB2B.201802090040'
GROUP BY cust_id||acct_no) mtnlist
FROM process.feature ffsr WHERE date_loaded BETWEEN TO_DATE('02-08-2018','MM-dd-yyyy')
AND TO_DATE('02-09-2018','MM-dd-yyyy') AND cust_id BETWEEN 0542185146 AND 0942025571
AND src_ind = 'B' AND filename = 'FEATURE.VB2B.201802090040'
AND letter_type = 'FA' ORDER BY cust_id;
With a little bit of help by XML, you might get it work. Example is based on HR schema.
SQL> select
2 listagg(s.department_name, ',') within group (order by null) result
3 from departments s, departments d;
from departments s, departments d
*
ERROR at line 3:
ORA-01489: result of string concatenation is too long
SQL>
SQL> select
2 rtrim(xmlagg(xmlelement (e, s.department_name || ',')).extract
3 ('//text()').getclobval(), ',') result
4 from departments s, departments d;
RESULT
--------------------------------------------------------------------------------
Administration,Administration,Administration,Administration,Administration,Admin
SQL>
This demo sourced from livesql.oracle.com
-- Create table with 93 strings of different lengths, plus one NULL string. Notice the only ASCII character not used is '!', so I will use it as a delimiter in LISTAGG.
create table strings as
with letters as (
select level num,
chr(ascii('!')+level) let
from dual
connect by level <= 126 - ascii('!')
union all
select 1, null from dual
)
select rpad(let,num,let) str from letters;
-- Note the use of LENGTHB to get the length in bytes, not characters.
select str,
sum(lengthb(str)+1) over(order by str rows unbounded preceding) - 1 cumul_lengthb,
sum(lengthb(str)+1) over() - 1 total_lengthb,
count(*) over() num_values
from strings
where str is not null;
-- This statement implements the ON OVERFLOW TRUNCATE WITH COUNT option of LISTAGG in 12.2. If there is no overflow, the result is the same as a normal LISTAGG.
select listagg(str, '!') within group(order by str) ||
case when max(total_lengthb) > 4000 then
'! ... (' || (max(num_values) - count(*)) || ')'
end str_list
from (
select str,
sum(lengthb(str)+1) over(order by str) - 1 cumul_lengthb,
sum(lengthb(str)+1) over() - 1 total_lengthb,
count(*) over() num_values
from strings
where str is not null
)
where total_lengthb <= 4000
or cumul_lengthb <= 4000 - length('! ... (' || num_values || ')');

ORA-00905: missing keyword error in case after where clause

I have the following query which is giving error ORA-00905: missing keyword. I've not been able to find the syntax despite continuous efforts for last few hours. Please help.
SELECT a.DOCUMENT_CATEGORY,
a.template_id,
a.category_id,
a.REVIEW_CATEGORY,
a.WITH_BIDS,
a.WITH_FINAL_DOCUMENTS,
b.divn_id,
b.deptt_id,
a.vdr_id,
C.DEPARTMENT,
a.TEMPLATE_TITLE
FROM DCTM_VDR_REF_DTLS a, DCTM_VDR_REF_MASTER b, VW_DIVN_DIR c
WHERE b.DIVN_ID = c.DIVN_CODE
AND b.DEPTT_ID = c.SECTN_CODE
AND a.vdr_id = b.vdr_id
AND (b.REFERENCE_NUMBER, b.APPROVED_ON) IN
( SELECT MAX (REFERENCE_NUMBER), MAX (APPROVED_ON)
FROM DCTM_VDR_REF_MASTER
WHERE REFERENCE_NUMBER =
(SELECT DISTINCT
NVL (TRIM (MR_NUMBER), TRIM (TENDER_NO))
FROM EILEDMS.EIL_DOCUMENT_SV#EDMS_DBLINK
WHERE object_name =
'A307-0IC-JA-MR-7960-1030-157-FOA'
AND r_object_type =
'eil_foa_order_pr_doc'
AND ( title = 'FOA'
OR title = 'DRAFT FOA'))
AND APPROVED_ON IS NOT NULL
GROUP BY DIVN_ID, DEPTT_ID)
AND REVIEW_CATEGORY <> 'Delete Category'
AND (CASE (SELECT IS_SCHEDULE_LOCKED
FROM DCTM_VENDOR_SCHEDULE
WHERE SCH_ID = 359)
WHEN 0
THEN
1
WHEN 1
THEN
(a.template_id || '-' || a.category_id) IN
(SELECT template_id || '-' || category_id
FROM DCTM_VENDOR_SCH_UNLOCK_DTLS
WHERE APPROVAL = 'Y'
AND APPROVAL_UPTO >= SYSDATE
AND CONSUMED = 0
AND sch_ID = 359)
END) = 1
ORDER BY c.DEPARTMENT ASC,
a.TEMPLATE_ID,
a.SORT_ORDER,
a.DOCUMENT_CATEGORY ASC
Can't we use IN clause inside a THEN statement?
Now that you've edited your question, it looks like you are simply trying to look up category_id and template_id in DCTM_VENDOR_SCH_UNLOCK_DTLS. Does the following work for you?
then
(
SELECT COUNT(*) -- 1 if found, 0 otherwise
FROM DCTM_VENDOR_SCH_UNLOCK_DTLS
WHERE APPROVAL = 'Y'
AND APPROVAL_UPTO >= SYSDATE
AND CONSUMED = 0
AND sch_ID = 359
AND template_id = a.template_id
AND category_id = a.category_id
AND rownum = 1
)
This is not actually about an IN clause after WHERE being allowed or not. The expression
a.category_id IN (SELECT ...)
evaluates to TRUE or FALSE. Your statement
a.template_id || '-' || a.category_id IN (SELECT ...)
tries to concatenate that TRUE or FALSE with a.template_id and a minus sign. This is not possible, as there is no boolean type in Oracle SQL. Think it over what you actually want to concatenate.
EDIT: Now that you set parentheses, you compare a string with another string resulting from a select statement. Fine so far. But still: All this evaluates to a boolean, not a number. Your first then results in a number (1), your second in a boolean (TRUE or FALSE). Oracle SQL has no boolean type, so your expression makes no sense to the parser and you get a syntax error.

Joining two SELECT statements using Outer Join with multiple alias

I have a complicated select statement that when it is executed, I give it a date or date range I want and the output comes out. The problem is I don't know how to join the same SQL statement with the first Statement having 1 date range and the second statement having another data range. Example below:
When I execute the select statement, I choose for the Month of November:
EMPLID NAME Current_Gross_Hours(November)
When I execute the select statement again, I choose from January to November:
EMPLID NAME Year_To_Date_Hours(January - November)
What I want:
EMPLID NAME Current_Gross_Hours(November) Year_To_Date_Hours(January - November)
The SQL Select statement runs correctly if execute by themselves. But I don't know how to join them.
Here is the SQL code that I want to write, but I don't know how to write the SQL statement correctly. Any help or direction is greatly appreciated.
(SELECT DISTINCT
SUM("PSA"."AL_HOURS") AS "Current Gross Hours", "PSJ"."EMPLID","PSP"."NAME"
FROM
"PS_JOB" "PSJ", "PS_EMPLOYMENT" "PSE", "PS_PERSONAL_DATA" "PSP", "PS_AL_CHK_HRS_ERN" "PSA"
WHERE
((("PSA"."CHECK_DT" = TO_DATE('2011-11-01', 'YYYY-MM-DD')) AND
("PSJ"."PAYGROUP" = 'SK2') AND
(("PSJ"."EFFSEQ"= (
SELECT MAX("INNERALIAS"."EFFSEQ")
FROM "PS_JOB" INNERALIAS
WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR"
AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID"
AND "INNERALIAS"."EFFDT" = "PSJ"."EFFDT")
AND
"PSJ"."EFFDT" = (
SELECT MAX("INNERALIAS"."EFFDT")
FROM "PS_JOB" INNERALIAS
WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR"
AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID"
AND "INNERALIAS"."EFFDT" <= SYSDATE)))))
AND
("PSJ"."EMPLID" = "PSE"."EMPLID" ) AND ("PSJ"."EMPLID" = "PSP"."EMPLID" ) AND ("PSJ"."FILE_NBR" = "PSA"."FILE_NBR" ) AND ("PSJ"."PAYGROUP" = "PSA"."PAYGROUP" ) AND ("PSE"."EMPLID" = "PSP"."EMPLID" )
GROUP BY
"PSJ"."EMPLID", "PSP"."NAME"
) AS "Q1"
LEFT JOIN
(SELECT DISTINCT
SUM("PSA"."AL_HOURS") AS "YEAR_TO_DATE Gross Hours", "PSJ"."EMPLID"
FROM
"PS_JOB" "PSJ", "PS_EMPLOYMENT" "PSE", "PS_PERSONAL_DATA" "PSP", "PS_AL_CHK_HRS_ERN" "PSA"
WHERE
((("PSA"."CHECK_DT" BETWEEN TO_DATE('2011-01-01', 'YYYY-MM-DD') AND TO_DATE('2011-11-01', 'YYYY-MM-DD')) AND
("PSJ"."PAYGROUP" = 'SK2') AND
(("PSJ"."EFFSEQ"= (
SELECT MAX("INNERALIAS"."EFFSEQ")
FROM "PS_JOB" INNERALIAS
WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR"
AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID"
AND "INNERALIAS"."EFFDT" = "PSJ"."EFFDT")
AND
"PSJ"."EFFDT" = (
SELECT MAX("INNERALIAS"."EFFDT")
FROM "PS_JOB" INNERALIAS
WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR"
AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID"
AND "INNERALIAS"."EFFDT" <= SYSDATE)))))
AND
("PSJ"."EMPLID" = "PSE"."EMPLID" ) AND ("PSJ"."EMPLID" = "PSP"."EMPLID" ) AND ("PSJ"."FILE_NBR" = "PSA"."FILE_NBR" ) AND ("PSJ"."PAYGROUP" = "PSA"."PAYGROUP" ) AND ("PSE"."EMPLID" = "PSP"."EMPLID" )
GROUP BY
"PSJ"."EMPLID"
) AS "Q2"
ON "Q1"."EMPLID"="Q2"."EMPLID"
ORDER BY
"Q1"."NAME"
You are missing a SELECT ... FROM at the start. The AS keyword only works when creating column aliases, not query block aliases. Quotation marks are only necessary for case-sensitive column names - they don't look incorrect in your example but they frequently cause mistakes.
SELECT Q1.NAME, ...
FROM
(
SELECT ...
) Q1
JOIN
(
SELECT ...
) Q2
ON Q1.EMPLID=Q2.EMPLID
ORDER BY Q1.NAME