Oracle Apex - Case within a where statement - sql

I'm having issues while running the following query (interactive report / simplified the query cause I'm sure the issue is with the case statement):
select
v.manager_email
from my_table v
where
(
case
when :P28_ACT_AS_ROLE_H = 'Director' then v.director_email = :P28_ACT_AS_H
when :P28_ACT_AS_ROLE_H = 'Admin' then v.manager_email = nvl(:P28_ACT_AS_H,
v.manager_email)
when :P28_ACT_AS_ROLE_H = 'Manager' then v.manager_email = :P28_ACT_AS_H
end
)
The error is this one: ORA-20999.
Does someone know why this is happening?
(:p28 items are calculated via computations and work perfectly)
Thanks!

Don't use a case when boolean logic suffices:
where (:P28_ACT_AS_ROLE_H = 'Director' and v.director_email = :P28_ACT_AS_H) or
(:P28_ACT_AS_ROLE_H = 'Admin' and v.manager_email = nvl(:P28_ACT_AS_H, v.manager_email)) or
(:P28_ACT_AS_ROLE_H = 'Manager' and v.manager_email = :P28_ACT_AS_H)
The specific reason in your example is that Oracle doesn't recognize a boolean expression as a valid return value for a case, resulting in a syntax error

Or, with CASE:
where nvl(:P28_CT_AS_H, v.manager_email) =
case when :P28_ACT_AS_ROLE_H = 'Director' then v.director_email
when :P28_ACT_AS_ROLE_H = 'Admin' then v.manager_email
when :P28_ACT_AS_ROLE_H = 'Manager' then v.manager_email
end

Related

Using a CASE WHEN with multiple Whens that is only resulting in a BOOLEAN value

I am working on writing a SQL Query which is using a CASE line with multiple When's, and I was wondering why my result is only showing BOOLEAN (True/False) Values. Ideally I would like it to show whatever is after the THEN's in my code.
--with loan as (
SELECT DISTINCT
rtw.identifierdimsk
,lmmf.milestonesk
,lmd.groupname
,rtw.typecode
,rtw.campaignname
,rtw.partnername
,rtw.sourcename
,lmmf.date
,rtw.typecode = CASE WHEN rtw.typecode = 'PROS' THEN 'Prospect'
WHEN rtw.typecode = 'CARI' THEN 'Deluxe'
WHEN rtw.typecode = ('CARIC') OR rtw.typecode = ('CTE') THEN 'CSignal'
WHEN rtw.typecode = 'RMT' THEN 'Pre Pull'
WHEN rtw.typecode in ('90','90PLUS','C90','C90PLUS','BP','ELO','AUTO','POP') THEN 'In-process'
WHEN rtw.typecode = 'XU3%' THEN 'U3'
WHEN rtw.typecode = '%CT%' THEN 'Misc'
END AS Value
FROM rtal_winner rtw
INNER JOIN one_fact lmmf ON lmmf.identifierdimsk = rtw.identifierdimsk
INNER JOIN one_dim lmd ON lmd.milestonesk = lmmf.milestonesk
AND lmmf.date >= '2022-10-25'
Because you are comparing rtw.typecode to the result of the case expression:
rtw.typecode = CASE ...
remove the comparison part:
, lmmf.date
, CASE
WHEN rtw.typecode = 'PROS' THEN 'Prospect'
-- ...
END AS Value
FROM rtal_winner rtw

case when using multiple conditions across multiple columns

So quick example:
I need to change that 'imperial' to 'Metric' in the UnitsofMeterRemoved Column. However it has to be when meter model = g4 and MeterManufacturerRemoved = KROMSCHRODER.
I have tried this
SELECT *,
CASE WHEN MeterModelRemoved = 'G4'
AND MeterManufacturerRemoved = 'KROMSCHRODER'
AND UnitsofMeterRemoved = 'Imperial'
THEN UnitsofMeterRemoved = 'Metric'
END AS MeterModelRemoved
FROM CTE_2
But the Imperial is still showing up? Any ideas why this may be happening?
use below
SELECT * EXCEPT(UnitsofMeterRemoved),
CASE WHEN MeterModelRemoved = 'G4'
AND MeterManufacturerRemoved = 'KROMSCHRODER'
AND UnitsofMeterRemoved = 'Imperial'
THEN 'Metric'
ELSE UnitsofMeterRemoved
END AS UnitsofMeterRemoved
FROM CTE_2

Is there a way to ignore the AND in a CASE when something is true?

I have this Where clause
Select * From Student_Info si
Inner Join Certifications cc
Inner Join Cert_Earned ce
Where si.grad_date = #grad_date
AND cc.org_no = #org_no
but I need an additional AND that should be ignored if it turns out the value is false, I will want ALL certificates
AND cc.industrial = CASE WHEN #industrial = 0 THEN Do Nothing
Else #industrial
This would normally be expressed as:
AND (#industrial = 0 OR ccc.industrial = #industrial)
It sounds like you just want to add a predicate that does an OR between two different conditions
AND (#industrial = 0 or ccc.industrial = #industrial)
You can do something like this with CASE functions:
AND 1 = (CASE WHEN #industrial = 0 THEN 1
ELSE CASE WHEN cc.industrial = #industrial THEN 1 END
END)
or if cc.industrial is not nullable, then maybe:
AND cc.industrial = CASE WHEN #industrial = 0 THEN cc.industrial
Else #industrial END

SQL Server Case in Where clause

I am trying to create a stored proc and have a where clause where to different operations can take place depending on the value of a parameter passed in:
WHERE
(cdr.CircuitReference = #CircuitReference)
AND
CASE WHEN (#JDEDocumentReference <> 'Unbilled Calls')
THEN
sct.JDEDocumentReference = #JDEDocumentReference
ELSE
((sct.JDEDocumentReference IS NULL) AND (sc.StartDate IS NOT null AND ((sc.CloseDate IS null) OR (datediff(day,sc.CloseDate,getdate()) < 0)) AND stp.SipTrunksProduct = sct.ProductCode))
END
I've just posted my where clause above but when i try to execute the script i get the following error:
Incorrect syntax near '='.
Is this the correct way to do a conditional statement in a where clause of an sql query?
Thanks
This problem could be solved without a CASE statement by using the following:
WHERE
(cdr.CircuitReference = #CircuitReference)
AND
((#JDEDocumentReference <> 'Unbilled Calls' AND sct.JDEDocumentReference = #JDEDocumentReference)
OR
(#JDEDocumentReference = 'Unbilled Calls' AND ((sct.JDEDocumentReference IS NULL) AND (sc.StartDate IS NOT null AND ((sc.CloseDate IS null) OR (datediff(day,sc.CloseDate,getdate()) < 0)) AND stp.SipTrunksProduct = sct.ProductCode))))
STATEMENT FULLY WRONG : There is no need for case here(Even there is a possibility to it correctly. But here no needed).
USE:
(cdr.CircuitReference = #CircuitReference)
AND ((JDEDocumentReference <> 'Unbilled Calls'
AND #JDEDocumentReference) OR #JDEDocumentReference = 'Unbilled Calls' )
OR (JDEDocumentReference = 'Unbilled Calls'
AND ((sct.JDEDocumentReference IS NULL) AND (sc.StartDate IS NOT null AND ((sc.CloseDate IS null)
OR (datediff(day,sc.CloseDate,getdate()) < 0)) AND stp.SipTrunksProduct = sct.ProductCode)))
You can use something like this,
WHERE
(cdr.CircuitReference = #CircuitReference)
AND sct.JDEDocumentReference = case when #JDEDocumentReference <> 'Unbilled Calls' Then #JDEDocumentReference end

Crystal Reports v14 not finding all null values

Alright, I've run into this before and have gotten everything to work correctly in the past. I have an SQL code that was created that now needs to be turned into a crystal report. The SQL shows 956 lines, but Crystal is only showing 886.
Here is the SQL code:
SELECT
I4240,
I4201,
I4202,
I4203,
I4204,
I4206,
I4213,
I4214,
I4225,
I4208,
I4299
FROM
MT.INVENTORY
WHERE
(
I4202 IN ('UNKNONWN','VERIFY MFR','OTHER','VARIOUS','TBD','NA','N/A') OR
(
I4203 IN ('UNKNONWN','VERIFY MODEL NUMBER','OTHER','VARIOUS','TBD','NA','N/A','MISCELLANEOUS')
OR I4203 IS NULL
OR LENGTH(I4203)=0
) OR
(
I4204 IN ('UNKNOWN DESCRIPTION','VERIFY DESCRIPTION','OTHER','VARIOUS','TBD','NONE - NO STD USED','NA','N/A','MISCELLANEOUS')
OR I4204 IS NULL
OR LENGTH(I4204)=0
)
) AND
I4240 NOT IN ('MT','STD','NESD')
ORDER BY I4240,I4202,I4203,I4204
and the record selection formula from CR:
(
{Inventory.I4240} <> 'mt' and
{Inventory.I4240} <> 'std' and
{Inventory.I4240} <> 'nesd'
)
AND
(
(
{Inventory.I4202} = 'UNKNONWN' OR
{Inventory.I4202} = 'VERIFY MFR' OR
{Inventory.I4202} = 'OTHER' OR
{Inventory.I4202} = 'VARIOUS' OR
{Inventory.I4202} = 'TBD' OR
{Inventory.I4202} = 'NA' OR
{Inventory.I4202} = 'N/A'
)
OR
(
{Inventory.I4203} = 'UNKNONWN' OR
{Inventory.I4203} = 'VERIFY MODEL NUMBER' OR
{Inventory.I4203} = 'OTHER' OR
{Inventory.I4203} = 'VARIOUS' OR
{Inventory.I4203} = 'TBD' OR
{Inventory.I4203} = 'NA' OR
{Inventory.I4203} = 'N/A' OR
{Inventory.I4203} = 'MISCELLANEOUS' OR
ISNULL({Inventory.I4203}) OR
LENGTH(trim({Inventory.I4203})) < 1 OR
INSTR(trim({Inventory.I4203}), "") = 0 OR
TRIM({Inventory.I4203}) = ""
)
OR
(
{Inventory.I4204} = 'UNKNOWN DESCRIPTION' OR
{Inventory.I4204} = 'VERIFY DESCRIPTION' OR
{Inventory.I4204} = 'OTHER' OR
{Inventory.I4204} = 'VARIOUS' OR
{Inventory.I4204} = 'TBD' OR
{Inventory.I4204} = 'NONE - NO STD USED' OR
{Inventory.I4204} = 'NA' OR
{Inventory.I4204} = 'N/A' OR
{Inventory.I4204} = 'MISCELLANEOUS' OR
ISNULL({Inventory.I4204}) OR
LENGTH(trim({Inventory.I4204})) < 1 OR
INSTR({Inventory.I4204}, "") = 0 OR
TRIM({Inventory.I4204}) = ""
)
)
Any help would be appreciated.
In Crystal, if a particular field can be null then you need to check for that condition as the very first thing you do with it, otherwise the entire formula will error out and nothing will be evaluated.
So in your case, fields I4203 and I4204 need the isnull() check moved to the top of their respective sections at the very least. If I4240 and I4202 can be null, then you should handle those conditions as well.
Also, you have a couple references to the word "UNKNONWN"; is that a typo?
The issue was that the report options were set to use null as default, but for some reason in the record selection formula it was set to exception. Once that was set to the default setting it worked without any issues and the counts were correct.