sql oracle - filter query sql using case when - sql

I have the below query
select
ens.ID_ENS,
ens.NOM_ENS,
-- for the first SURVEILLANT
disp.SALLE_EXAM,
disp.NB_HEURES_ENS,
-- for the second SURVEILLANT
disp.SALLE_EXAM2,
disp.NB_HEURES_ENS2
from ESP_ENSEIGNANT ens, ESP_MODULE_PANIER_CLASSE_SAISO disp
WHERE ens.ID_ENS IN (disp.SURVEILLANT, disp.SURVEILLANT2) AND ens.NOM_ENS != 'A AFFECTER';
I want update the query for display SALLE_EXAM, NB_HEURES_ENS only for SURVEILLANT
select
ens.ID_ENS,
ens.NOM_ENS,
CASE
WHEN ens.ID_ENS = disp.SURVEILLANT THEN (disp.SALLE_EXAM AS "EXAM", disp.NB_HEURES_ENS AS "HOURDISP")
WHEN ens.ID_ENS = disp.SURVEILLANT2 THEN (disp.SALLE_EXAM2 AS "EXAM", disp.NB_HEURES_ENS2 AS "HOURDISP")
END
from ESP_ENSEIGNANT ens, ESP_MODULE_PANIER_CLASSE_SAISO disp
WHERE ens.ID_ENS IN (disp.SURVEILLANT, disp.SURVEILLANT2) AND ens.NOM_ENS != 'A AFFECTER';

You can rewrite the query with explicit JOIN syntax as
SELECT ens.ID_ENS,
ens.NOM_ENS,
CASE
WHEN ens.ID_ENS = disp.SURVEILLANT THEN
disp.SALLE_EXAM
WHEN ens.ID_ENS = disp.SURVEILLANT2 THEN
disp.SALLE_EXAM2
END AS "EXAM",
CASE
WHEN ens.ID_ENS = disp.SURVEILLANT THEN
disp.NB_HEURES_ENS
WHEN ens.ID_ENS = disp.SURVEILLANT2 THEN
disp.NB_HEURES_ENS2
END AS "HOURDISP"
FROM ESP_ENSEIGNANT ens
JOIN ESP_MODULE_PANIER_CLASSE_SAISO disp
ON ens.ID_ENS IN (disp.SURVEILLANT, disp.SURVEILLANT2)
AND ens.NOM_ENS != 'A AFFECTER';
A side note : if disp.SURVEILLANT and disp.SURVEILLANT2 have equal values, then disp.SALLE_EXAM and disp.NB_HEURES_ENS will be picked respectively. Eg the first components have precedence.

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

Oracle Apex - Case within a where statement

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

How to select make the default value of all my variables '0' when only nulls are being returned?

So I am running into issues where my conditional statements don't work because I am doing comparisons (at times for certain situations) where I'm comparing a null with an integer. I tried using an exception but after printing the variable values onto my display i found out that this exception only handled 'NO_DATA_FOUND' error messages. Is there a way I can get the variable to get a '0' selected into it in the case a 'NULL' or BLANK is returned? Below is my code:
SELECT MAX(VALUE)
INTO VARIABLE_1
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'HI';
SELECT MAX(VALUE)
INTO VARIABLE_2
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'BYE';
---
EXCEPTION
WHEN no_data_found
THEN SELECT 0
INTO VARIABLE_1
FROM DUAL;
SELECT 0
INTO VARIABLE_2
FROM DUAL;
You can use nvl() to provide a default value:
SELECT NVL(MAX(VALUE), 0)
INTO VARIABLE_1
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'HI';
SELECT NVL(MAX(VALUE), 0)
INTO VARIABLE_2
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'BYE';
The no_data_found exception won't be raised because you're using an aggregate function; that will always return one row, even its value is null. A null value is not the same as no rows being returned.
Or you can use coalesce
SELECT coalesce(MAX(VALUE), 0) AS value
INTO VARIABLE_1
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'HI';
SELECT coalesce(MAX(VALUE), 0) AS value
INTO VARIABLE_2
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'BYE';
use nvl command for it like :
nvl(column_name,0).

query is returning " single-row subquery returns more than one row"

I am not sure why the below query is giving the " single-row subquery returns more than one row" please let me know if I am missing anything.
CASE
WHEN s.servprov_gid = 'IFFCO.CAR-60041'
THEN
(SELECT E.EQUIPMENT_NUMBER
FROM S_EQUIPMENT SE,
EQUIPMENT E,
SHIPMENT_S_EQUIPMENT_JOIN SSEJ,
SHIPMENT S
WHERE SSEJ.SHIPMENT_GID = 'IFFCO/LOGISTICS.L171203007'
AND SE.S_EQUIPMENT_GID = SSEJ.S_EQUIPMENT_GID
AND E.EQUIPMENT_GID = SE.EQUIPMENT_GID
AND SSEJ.SHIPMENT_GID = S.SHIPMENT_GID
)
ELSE
(SELECT MIN(se.equipment_number)
FROM shipment_s_equipment_join ssej,
s_equipment se
WHERE ssej.shipment_gid = 'IFFCO/LOGISTICS.L171203007'
AND ssej.s_equipment_gid = se.s_equipment_gid
AND se.equipment_number IS NOT NULL
)
END
The error message advise very clearly, more than one row are returned from the sub-query after the key word "Then". You can add "Top 1" after the first SELECT keyword for SQL Serve4.
Or get the sub query end with "and rownum =1" for Oracle. The following is for SQL Server.
CASE
WHEN s.servprov_gid = 'IFFCO.CAR-60041'
THEN
(SELECT TOP 1 E.EQUIPMENT_NUMBER
FROM S_EQUIPMENT SE,
EQUIPMENT E,
SHIPMENT_S_EQUIPMENT_JOIN SSEJ,
SHIPMENT S
WHERE SSEJ.SHIPMENT_GID = 'IFFCO/LOGISTICS.L171203007'
AND SE.S_EQUIPMENT_GID = SSEJ.S_EQUIPMENT_GID
AND E.EQUIPMENT_GID = SE.EQUIPMENT_GID
AND SSEJ.SHIPMENT_GID = S.SHIPMENT_GID
)
ELSE
(SELECT MIN(se.equipment_number)
FROM shipment_s_equipment_join ssej,
s_equipment se
WHERE ssej.shipment_gid = 'IFFCO/LOGISTICS.L171203007'
AND ssej.s_equipment_gid = se.s_equipment_gid
AND se.equipment_number IS NOT NULL
)
END

return string value from SQL Server tables where column type is integer

In my mssql tables i have a clomun that data type is integer. and when i was try thats you know the return value is int.
select h.hekimlik_kod, h.hekim_adi, h.hekim_soyadi, h.il_kod, h.ilce_kod, h.islem_tarihi, h.durum
h.asm, il.aciklama as il_adi, ilce.aciklama as ilce_adi from hekim as h
left join il_ilce as il on il.ust_kod = 0 and il.kod = h.il_kod
left join il_ilce as ilce on ilce.ust_kod = h.il_kod and ilce.kod = h.ilce_kod
int this sql statement "h.durum"' data type is integer.
but i want to make thats.
when h.durum is 0 return string is Yönetici
when h.durum 1 string is Normal Kullanıcı
how can i do in sql?
thank you for patience
Not sure if I understood the question, but try replacing h.durum on your SELECT with the following:
CASE WHEN h.durum = 0 THEN 'Yönetici'
WHEN h.durum = 1 THEN 'Normal Kullanıcı'
ELSE '???' END AS some_column_alias
Try this:
select
CASE
WHEN h.durum = 0 THEN N'Yönetici'
WHEN h.durum = 1 THEN N'Normal Kullanıcı'
END CASE ColumnName,
h.hekimlik_kod, h.hekim_adi, h.hekim_soyadi, h.il_kod, h.ilce_kod, h.islem_tarihi, h.durum
h.asm, il.aciklama as il_adi, ilce.aciklama as ilce_adi
FROM hekim as h
left join il_ilce as il on il.ust_kod = 0 and il.kod = h.il_kod
left join il_ilce as ilce on ilce.ust_kod = h.il_kod and ilce.kod = h.ilce_kod
;