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;
Related
I am new to SQL and trying to use an if statement equivalent in order to get the value in SQL.
After running this report I am getting the error "ORA-00936: missing expression"
CASE WHEN (select Sum(MONTANT) from fraisExterne where matching=fm.id and statut=3) IS NULL
THEN select sum(MONTANT) from fraisExterne where matching=fm.id
ELSE Select sum (MONTANTHTBROKER) from fraismatching where matching=fm.id
End End as "BROKER AMOUNT"
Please let us know if you can help me with it
You have a extra END key word at the end of your case statement. You also need to put your select query into ().
Try this:
SELECT
CASE
WHEN (select Sum(MONTANT) from fraisExterne where matching=fm.id and statut=3) IS NULL THEN (select sum(MONTANT) from fraisExterne where matching=fm.id)
ELSE (Select sum (MONTANTHTBROKER) from fraismatching where matching=fm.id)
End AS BROKER_AMOUNT
I hope this helps and welcome to StackOverflow. If you find this answer or any other answer solves your problem please mark it as the solution. This will help the community and fellow programmers who run into the same problem in the future. Thanks.
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.
I’m using SAP Lumira desktop and "Query with SQL (Freehand SQL)", connected to SAP ECC. I try to extend the query with a case statement but run into errors as below:
SELECT "VBUK-UVALS",
CASE ("VBUK-UVALS") WHEN 'A' THEN 'Closed'
WHEN 'B' THEN 'Open'
ELSE 'Other'
END AS "ColumnA"
FROM "Local"."INFOSET"."ZCA_TESTAR"
Syntax error in SQL query:
[line 2:31 missing FROM at 'WHEN'][line 2:36 missing EOF at "A"]
It would be very appreciated if any could guide me through this
As the error suggests, your syntax is wrong. A case expression has only one case keyword, and can have multiple when clauses of the different values you're evaluating (and an optional single else clause):
SELECT "VBUK-UVALS",
CASE ("VBUK-UVALS") WHEN 'A' THEN 'Closed'
WHEN 'B' THEN 'Open' -- CASE ("VBUK-UVALS") removed
ELSE 'Other'
END AS "ColumnA"
FROM "Local"."INFOSET"."ZCA_TESTAR"
Sorry, to many “CASE”, It was a mistake by me when wrote the case.
The Query is stated as followed with the same error message
SELECT "VBUK-UVALS",
CASE ("VBUK-UVALS") WHEN 'A' THEN 'Closed'
WHEN 'B' THEN 'Open'
ELSE 'Other'
END AS "ColumnA"
FROM "Local"."INFOSET"."ZCA_TESTAR"
Syntax error in SQL query:
[line 2:27 missing FROM at 'WHEN'][line 2:32 missing EOF at "A"]
This is the part of my query that has error:
, case when dsi.distributor_id in
('ALBQA','ASGLA','ASGNY','ASGR1','ASGSF','BIKU9','COAUU','CSWHP','DPIB1','DPID9',
'DPISP','DPISQ','EAS3X','GEP79','GRG8V','NACY7','NOSYK','ORGK7','PETR1','TOP0U',
'UNFIA','UNFIL','UNFIQ','UNFIS','UNMQ9','KOSI8','KEHEN','CSNYC','ALBQA','ALC6Y','BAM7D','BIKU9','CLCE0','COAUU','CSWHT','EAS3X','FOUXU','GEP79',
'GRG8V','HED9Q','LAOJD','MCLFS','NOSYK','ORGK7','UNMQ9','OMAH1'
)
then 'Distributor'
else 'Direct'
end as is_direct
, SUM(dsi.cost) AS tot_cost
, SUM(CASE WHEN is_direct = 'Direct' THEN dsi.cost ELSE 0 END ) AS Direct_cost
It says that is_direct is an invalid column but I already indicated it above. Therefore, I was wondering if you could help me find where I went wrong.
Columns defined in your query cannot be used in other places in your query, whether it is in other columns, or WHERE clauses, etc.
I would suggest either placing these calculations into subqueries, or using WITH AS.
When I trying the below SQL I am getting the error ORA-00936: missing expression. Please help me on this, I want distinct on those to columns in Oracle SQL
SELECT rr.RPT_QUE_I,
DISTINCT (rr.ed_sbmt_m, rr.RPT_RUN_STAT_C),
rr.rpt_cstm_x,
rr.rpt_cmplt_m,
CASE
WHEN rr.rpt_run_stat_c = 25453 THEN 'PENDING'
WHEN rr.rpt_run_stat_c = 25454 THEN 'ACTIVE'
WHEN rr.rpt_run_stat_c = 25455 THEN 'FINISHED'
WHEN rr.rpt_run_stat_c = 25458 THEN 'ERROR'
WHEN rr.rpt_run_stat_c = 25460 THEN 'SCHEDULED'
END,
cc.pro_sym_I,
rr.usr_wad_ownr_i
FROM audit_REPORT_RUN rr,
CLIENT_COMPANY cc
WHERE rr.ED_SBMT_M > TO_DATE('06/01/2012','mm/dd/yyyy')
AND rr.ED_SBMT_M < TO_DATE('07/01/2012','mm/dd/yyyy')
AND rr.ORG_I = cc.ORG_I
ORDER BY rr.RPT_QUE_I
You should use DISTINCT keyword at first after the SELECT keyword, and remove the parenthesis,
or if you need to categorize some of it, use GROUP BY functions. hope you're getting the desired result.