ORA-00905: Missing keyword in CASE expression - sql

I am getting this error "ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
Error at Line: 20 Column: 76"
While trying to run the following query.
WITH t as (
SELECT tot.student_id,
tot.first_name,
tot.last_name,
tot.track_date,
MAX(track_date) over (partition by tot.student_id) as max_track_date
from student tot
inner join student_backup toov
on tot.student_id = toov.student_id
AND CASE WHEN tot.track_date IS NULL THEN SYSDATE ELSE tot.track_date between
toov.start_dt AND toov.start_dt + toov.duration/60/40 END
where toov.course_id=163121)
SELECT * FROM t;
Please help me regarding this. Thanks in advance

...
AND CASE WHEN tot.track_date IS NULL THEN SYSDATE ELSE tot.track_date between
toov.start_dt AND toov.start_dt + toov.duration/60/40 END
What is that supposed to do? You have a CASE expression, WHEN... THEN... and you get to the ELSE clause. Right after ELSE you have a column name and then the keyword BETWEEN. That makes no sense. BETWEEN is a logical operator, what is it doing there?
On further reading: it seems you misplaced the keyword END. It belongs before the keyword BETWEEN.

Related

ORA-00907: missing right parenthesis 00907. 00000 - "missing right parenthesis", while on a subquery with all parenthesis matching

Hi I have been trying to do a subquery using 2 tables and when I try to run the following code:
select t.cantidad_traslado,
(select sum(te.coste)"Coste",
(case when te.nif_emptransporte='F-98987667-R' then'AceSur'
when te.nif_emptransporte='A-98985367-V' then'TransMadrid'
when te.nif_emptransporte='A-97654567-S' then'Perez e Hijos'
when te.nif_emptransporte='A-87684567-B' then'Resur'
when te.nif_emptransporte='A-98987067-V' then'HuelResi'
else 'Indefinido'
end)"Empresa"
from traslado_empresatransportista te
group by te.nif_emptransporte
where t.nif_empresa=te.nif_empresa)
from traslado t
where t.nif_empresa in (select distinct nif_empresa from traslado_empresatransportista)
but when I try to run it oracle shows me the following error:
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error at Line: 12 Column: 9.
I am pretty sure I have all of my parenthesis right, so I wanted to know if yall could help me.
Edit:
Someone suggestes I swapped the order between the where and group by clause like this:
select t.cantidad_traslado,
(select sum(te.coste)"Coste",
(case when te.nif_emptransporte='F-98987667-R' then'AceSur'
when te.nif_emptransporte='A-98985367-V' then'TransMadrid'
when te.nif_emptransporte='A-97654567-S' then'Perez e Hijos'
when te.nif_emptransporte='A-87684567-B' then'Resur'
when te.nif_emptransporte='A-98987067-V' then'HuelResi'
else 'Indefinido'
end)"Empresa"
from traslado_empresatransportista te
group by te.nif_emptransporte
where t.nif_empresa=te.nif_empresa)
from traslado t
where t.nif_empresa in (select distinct nif_empresa from traslado_empresatransportista)
When I did I was presented the following error:
ORA-00913: too many values
00913. 00000 - "too many values"
*Cause:
*Action:
From Oracle 12, you can use a LATERAL join and then return multiple columns from the joined table:
select t.cantidad_traslado,
te.*
from traslado t
CROSS JOIN LATERAL (
select sum(te.coste) AS "Coste",
case
when te.nif_emptransporte='F-98987667-R' then'AceSur'
when te.nif_emptransporte='A-98985367-V' then'TransMadrid'
when te.nif_emptransporte='A-97654567-S' then'Perez e Hijos'
when te.nif_emptransporte='A-87684567-B' then'Resur'
when te.nif_emptransporte='A-98987067-V' then'HuelResi'
else 'Indefinido'
end AS "Empresa"
from traslado_empresatransportista te
where t.nif_empresa=te.nif_empresa
group by te.nif_emptransporte
) te

Oracle SQL Error : 00909. 00000 - "invalid number of arguments" in SELECT subquery

this is my first time using oracle Database in the company and I encountered this problem.
SELECT w.TABLE_NAME tbl_nm,
MAX(CASE WHEN t.partition_range LIKE 'year' THEN SUBSTR((w.PARTITION_NAME),1, LENGTH(w.PARTITION_NAME)-4) ELSE concat(SUBSTR((w.PARTITION_NAME), 1, LENGTH(w.PARTITION_NAME)-6)) END) AS par_nm,
MAX(CASE WHEN t.partition_range LIKE 'year' THEN SUBSTR((w.PARTITION_NAME), LENGTH(w.PARTITION_NAME)-3,4) ELSE concat(SUBSTR((w.PARTITION_NAME), LENGTH(w.PARTITION_NAME)-5,6)) END) AS par_date,
t.partition_range par_range,
t.data_dir data_dir,
t.index_dir index_dir,
t.add_range add_range,
CASE WHEN t.partition_range LIKE 'year' THEN EXTRACT(YEAR FROM systimestamp) ELSE TO_CHAR(systimestamp, 'YYYYMM') END AS cur_date
FROM INFORMATION_SCHEMA.PARTITIONS w, t_partition_manage t
WHERE w.TABLE_NAME = t.TABLE_NAME
GROUP BY w.TABLE_NAME
when I Select tbl_nm, par_nm, par_date, par_range, data_dir, index_dir, add_range, cur_date from INFORMATION_SCHEMA.PARTITIONS and t_partition_manage,
there seem to be a problem around in MAX( ... ) and I don't know what is wrong...
This is the error code
00909. 00000 - "invalid number of arguments"
*Cause:
*Action:
and I'm using SQLDeveloper as a client.
There is a problem with concat as it takes two parameters and You are passing only one parameter. Concat is not required at all in your solution:
SUBSTR(w.PARTITION_NAME, 1, LENGTH(w.PARTITION_NAME)-6)
And
SUBSTR(w.PARTITION_NAME), LENGTH(w.PARTITION_NAME)-5,6)
Also, you need more columns in GROUP BY as select can not use columns directly which are not in GROUP BY clause or you can use aggregate function.
Looks like CONCAT; it expects two parameters, while yours has only one.
CONCAT (SUBSTR ((w.PARTITION_NAME), 1, LENGTH (w.PARTITION_NAME) - 6))

Why is my case statement not working with a calculation while using signed over punch?

I'm trying to write a query to go against "Signed Over Punch" using the following query:
SELECT
CASE when substring(MyField,16,1)='C' then cast(substring(MyField,9,7)+'0' AS decimal(20,2)*-1 FROM MyTable
Here's some sample data:
0000069A0000006C00000000#0000000#
From the above data, the position starts at 16 ("C") with a length of 1
And the other starts at position 9 (0) with a length of 7
But I keep getting this error:
Msg 102, Level 15, State 1, Line 139
Incorrect syntax near '*'.
Desired Output:
00000063 (The C = 3)
What am I doing wrong?
Please refer to this page for reference for signed over punch:
https://en.wikipedia.org/wiki/Signed_overpunch
You need to learn about operator precedence. This code:
[..snip..] then cast(substring(MyField,9,7)+'0' AS decimal(20,2)*-1
is executing as if it had been written
[..snip..] then cast(...) AS (decimal(20,2) * -1)
^------------------^
You're not multiplying the result of the cast, you're trying to mutiply the decimal(20,2), which is NOT a multiplicable value.
Try
then (cast(substring(MyField,9,7)+'0' AS decimal(20,2)) * -1
^------------------------------------------------^
instead.
Not sure if this will fix it or not, but you are missing a closing parenthesis. You are also missing the END statement I recommend indenting things like this to make it easier to spot these problems.
SELECT CASE
WHEN substring(MyField,16,1)='C'
THEN cast(substring(MyField,9,7)+'0' AS decimal(20,2))*-1
END
FROM MyTable
try so:
SELECT CASE when substring(MyField,16,1)='C' then cast(substring(MyField,9,7)+'0' AS decimal(20,2)) *-1 end FROM MyTable
The problems were a missing " ) " at the defore " *-1 " and a missing "end" to terminate the case when condition.
With these 2 fix the result for your example '0000069A0000006C00000000#0000000#' is -60.00.

Multiple cases in Oracle sql. Getting the following error

I'm getting 00923. 00000 "FROM keyword not found where expected" error while using case. can you help me with multiple cases. How to use them and how to resolve below case
SELECT WorkflowHistory.*,
Reason.Reason,
ApprovalType.ApprovalType
CASE
WHEN NVL(APPROVALTYPE,'')=''
THEN 'APPROVALTYPE' = XPURPOSEFORREJECTION
END
CASE
WHEN NVL(daction,'')='sendTo'
THEN 'daction' = 'Approve'
END
FROM WorkflowHistory WorkflowHistory,
Reason Reason,
ApprovalType ApprovalType
WHERE UPPER(dDocName) = UPPER('D_1238777')
AND xPurposeForSubmission = Reason.ReasonID(+)
You need to add commas to separate your case expressions, and add optional field names. The assignment syntax needs to be changed like this:
SELECT WorkflowHistory.*,
Reason.Reason,
ApprovalType.ApprovalType
CASE
WHEN APPROVALTYPE IS NULL
THEN XPURPOSEFORREJECTION
END AS APPROVALTYPE,
CASE
WHEN NVL(daction,'')='sendTo'
THEN 'Approve'
END AS daction
FROM WorkflowHistory WorkflowHistory,
Reason Reason,
ApprovalType ApprovalType
WHERE UPPER(dDocName) = UPPER('D_1238777')
AND xPurposeForSubmission = Reason.ReasonID(+)
Your query appears to be missing join criteria for the ApprovalType table. Also consider switching to ANSI joins from Oracle-specific (+) syntax.

Is it possible to use CASE with ANY() in Oracle SQL?

Is possible to achieve what I'm trying to do in the below SQL query?
I want to check if the item name matches any of the given list.
select
case item_name
when any ('PEN', 'PENCIL', 'PAPER', 'ERASER') then 'STATIONARY'
else 'OTHER'
end is_stationary
from items;
I tried this query, and seems it is not possible. The error message is,
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Error at Line: 29 Column: 10
Is there any other workaround to achieve this?
Try this
select
case when item_name IN ('PEN', 'PENCIL', 'PAPER', 'ERASER') then 'STATIONARY'
else 'OTHER'
end is_stationary
from items;
CASE .. IN .. REFERENCE LINK
#bvr's answer is by far the most common way to solve this problem. But it looks like you were very close to correctly using an alternative syntax, the
group comparison condition.
select
case
when item_name = any ('PEN', 'PENCIL', 'PAPER', 'ERASER') then 'STATIONARY'
else 'OTHER'
end is_stationary
from items;