ORACLE SQL // IF ELSE? - oracle

I got a small problem in my SQL code :
LEFT JOIN
(SELECT *
FROM pilotage_usines_valeurs
WHERE c_indicateur IS NOT NULL) v
ON v.id_usine = d.id_usine
AND v.annee = 2015
AND V.MOIS = D.MOIS
AND V.C_INDICATEUR = pi1.c_indicateur
Sometime pi1.c_indicateur is null. How can i test it and write the line if pi1.c_indicateur is not null don't write it if pi1.c_indicateur is null ?

If I understand your intentions correctly I suggest you add the NOT NULL condition to the ON clause:
LEFT JOIN (SELECT *
FROM PILOTAGE_USINES_VALEURS
WHERE C_INDICATEUR IS NOT NULL) v
ON v.ID_USINE = d.ID_USINE
AND v.ANNEE = 2015
AND v.MOIS = D.MOIS
AND v.C_INDICATEUR = pi1.C_INDICATEUR
AND pi1.C_INDICATEUR IS NOT NULL

NVL is used to substitute null value by something else. Syntax is similar to
NVL (FIELD_TO_BE_TESTED_FOR_NULL, VALUE_IF_NULL);
Option 1:
Replace pi1.c_indicateur when null with some value so that V.C_INDICATEUR will never be equal to that value.
AND V.C_INDICATEUR = NVL(pi1.c_indicateur,'IMPOSSIBLE VALUE FOR V.C_INDICATEUR ');
Option 2:
Replace V.C_INDICATEUR with some value if null and replace pi1.c_indicateur with some other value, so that both will never match if NULL.
AND (V.C_INDICATEUR,'ABC') = NVL(pi1.c_indicateur,'DEF');

Related

UPDATE sentence using WITH doesn't work in MonetDB

I have this UPDATE in MonetDB (version 11.41.11):
with t as (select distinct cod_atc_nl_1 , desc_atc_nl_1 from uruguay.staging_rm_dims where desc_atc_nl_1 is not null order by 1)
update uruguay.staging_rm_dims
set
desc_atc_nl_1 = t.desc_atc_nl_1,
desc_atc_nl_2 = t.desc_atc_nl_1,
desc_atc_nl_3 = t.desc_atc_nl_1,
desc_atc_nl_4 = t.desc_atc_nl_1
where
desc_atc_nl_1 is null
and desc_atc_nl_2 is null
and desc_atc_nl_3 is null
and desc_atc_nl_4 is null
and cod_atc_nl_4 is not null
and cod_atc_nl_1 = t.cod_atc_nl_1;
When I executing this sentence, I get the following error:
SQL Error [42S22]: SELECT: no such column 't.cod_atc_nl_1'
Is sintax incorrect?
Thank you.
This was an issue fixed at the version 11.41.1. The with relation must be specified on the from clause.

why count(*) is slow even with index?

This is my query:
select count(*)
FROM TB_E2V_DOCUMENTOS_CICLO D
WHERE (D.TIPOCLIENTE = null or null is null)
AND (D.TIPODOCUMENTOCLIENTE = null or null is null)
AND (D.NUMDOCUMENTOCLIENTE = null or null is null)
AND (D.BA = null or null is null)
AND (D.FA = null or null is null)
AND (D.NOMBRECLIENTE = null or null is null)
AND (D.NUMTELEFONO = null or null is null)
AND (D.NUMSUSCRIPCION = null or null is null)
AND (D.TIPORECIBO in ('Recibo'))
AND (D.NUMRECIBO = null or null is null)
AND (TO_DATE(D.FECHAEMISION, 'yyyy/MM/dd') BETWEEN TO_DATE('2019-5-1', 'yyyy-MM-dd') AND TO_DATE('2020-2-18', 'yyyy-MM-dd'))
AND (D.MONTORECIBO = null or null is null)
AND (D.NUMPAGINAS = 0 or 0 = 0)
AND (D.NOMBREARCHIVO = null or null is null)
AND (D.NEGOCIO = null or null is null)
AND (D.NOMBREMETADATACARGA = null or null is null)
AND (D.FECHACARGA = TO_DATE(null) or TO_DATE(null) is null);
This query returns
And when I do a Xplain For:
The cost is very high, but this query uses the index. The query lasts 10 seconds approximately.
How can I improve the performance of the query?
I'm using Oracle 12c
Notes: All of the " and ( = null or null is null)" predicates will always evaluate to true; Oracle does not define null so null does not equal null, so instead if you want to check for null then use "is null"
select * from dual where null = null; -- returns no rows
select * from dual where not (null <> null); -- returns no rows
select * from dual where null is null; -- returns 1 row
select * from dual where not(null is not null); -- returns 1 row
As far as indexing goes, you need an index that is selective (i.e. return much fewer rows) and is present in the where clause predicate. In this case it looks like a function-based index on TO_DATE(D.FECHAEMISION, 'yyyy/MM/dd')
along with D.TIPORECIBO is in order. The INDEX SKIP SCAN is used in this case probably because D.TIPORECIBO is not the leading column; INDEX SKIP SCANs are slower then INDEX RANGE SCANs because it needs to read more index blocks.
There are a few factors involved here:
First, this query is using the second (or third) part of a composite index, resulting in the SKIP SCAN.
Take a look at all indexes on the table and see what kind of index is on TIPORECIBO.
It is likely that this isn't the leading column. You might improve the performance by creating an index with TIPORECIBO as leading column, but it is unlikely--this appears to be a "type" column that might have only a few values, and not a good candidate for an index.
The second issue is that Oracle uses the index to get a set of candidate rows, then goes to the data blocks themselves to get the rows for further filtering.
A select count(*) will perform much better if Oracle doesn't need to fetch the data blocks. This can be achieved by creating an index that contains all of the data needed for the filter.
In your case, an index on TIPORECIBO and FECHAEMISION would mean that Oracle could go to the index alone without needing to access the data blocks.
The third issue is that you are applying TO_DATE to the FECHAEMISION column. If this is a DATE datatype, then you don't need the conversion and it is causing you trouble. If you do need the conversion, an option would be a function-based index on TO_DATE(D.FECHAEMISION, 'yyyy/MM/dd').
To tune this particular query, you can try a function-based composite index:
CREATE INDEX TB_E2V_DOCUMENTOS_CICLO_FX1 ON TB_E2V_DOCUMENTOS_CICLO(FECHAEMISION, TO_DATE(D.FECHAEMISION, 'yyyy/MM/dd'))
Finally, this query is clearly being generated from code:
lines like AND (D.BA = null or null is null) seem to be a way of excluding portions of the WHERE clause when the front-end passes a NULL. This would possibly be AND (D.BA = 'X' or 'X' is null) if a value were provided for that parameter.
As such, be careful when tuning for the current set of parameters, as any change in what generated this query will impact the effectiveness of your tuning.
If you have a way to influence how this query is generated, it would be nice to simply exclude those non-event filters when the values are not provided, though Oracle ought to be able to handle them as-is.

Oracle procedure 'with query' insert into table

I have created an Oracle SQL query in TOAD which works fine. I now need to put this in a procedure.
The query has to create two counts based on different criteria (I have used With Select) and insert these plus a date and location to a table.
The query that works is
with
Selected_animals as
( SELECT TO_CHAR(SYSDATE,'DD/MM/YYYY' ) as Report_Date,
loc.name location,
count(rran.id) as Count_exported
FROM rr_animals rran,
contact con,
locations loc,
names nam
WHERE con.connum = rran.connum
AND con.loc_id = loc.id
AND con.connum = nam.connum
AND nam.name_type = 'STAND'
AND nam.dob IS NOT NULL
AND rran.sex IS NOT NULL
AND rran.web_display = 'Y'
AND rran.web_description IS NOT NULL
AND rran.visit_end_date IS NULL
AND con.loc_id IS NOT NULL
AND con.datedl IS NULL
AND rran.hold_user IS NULL
AND rran.assess_status IS NULL
AND EXISTS (SELECT rrim.id
FROM rr_images rrim
WHERE rrim.image_type = 'KENNEL'
AND rrim.rran_id = rran.id
AND DBMS_LOB.GETLENGTH(rrim.image_object) >0
AND rrim.image_object IS NOT NULL)
group by loc.NAME ),
total_animals as
(select vbav.sitename as location,
count(vbav.rran_ID) as Count_available
from v_bx_all_animal_visits vbav
where visit_end_date is null
and concat != 'DELTD'
and concat != 'DSCD'
group by vbav.sitename)
select Total_animals.location,
Selected_animals.Report_date,
Selected_animals.count_exported,
Total_animals.count_available
from Selected_animals, total_animals
where total_animals.location = selected_animals.location(+)
I have looked at several ways that seem to write the procedure but nothing seems to work. Including which was added under the CREATE or REPLACE and before BEGIN:
( o_location out bx_webstats_export_available.LOCATION%TYPE,
o_date out bx_webstats_export_available.REPORT_DATE%TYPE,
o_exported out bx_webstats_export_available.COUNT_EXPORTED%TYPE,
o_available out bx_webstats_export_available.COUNT_AVAILABLE%TYPE )
Also added after the last where statement and before End:
INSERT INTO bx_webstats_export_available(location, report_date, count_export, count_available)
values (Total_animals.location,
Selected_animals.Report_date,
Selected_animals.count_exported,
Total_animals.count_available);
Can anyone help me get this query in a Procedure please?
This is the first time I have written a Procedure from scratch and I'm struggling with it.
Many thanks,
What is it you're trying to do? Insert the results of that select into a table? If so, the following ought to suffice:
create or replace procedure your_proc_name
as
begin
insert into bx_webstats_export_available(location, report_date, count_export, count_available)
with selected_animals as (select to_char(sysdate,'DD/MM/YYYY' ) as report_date,
loc.name location,
count(rran.id) as count_exported
from rr_animals rran,
contact con,
locations loc,
names nam
where con.connum = rran.connum
and con.loc_id = loc.id
and con.connum = nam.connum
and nam.name_type = 'STAND'
and nam.dob is not null
and rran.sex is not null
and rran.web_display = 'Y'
and rran.web_description is not null
and rran.visit_end_date is null
and con.loc_id is not null
and con.datedl is null
and rran.hold_user is null
and rran.assess_status is null
and exists (select rrim.id
from rr_images rrim
where rrim.image_type = 'KENNEL'
and rrim.rran_id = rran.id
and dbms_lob.getlength(rrim.image_object) >0
and rrim.image_object is not null)
group by loc.name),
total_animals as (select vbav.sitename as location,
count(vbav.rran_id) as count_available
from v_bx_all_animal_visits vbav
where visit_end_date is null
and concat != 'DELTD'
and concat != 'DSCD'
group by vbav.sitename)
select total_animals.location,
selected_animals.report_date,
selected_animals.count_exported,
total_animals.count_available
from selected_animals, total_animals
where total_animals.location = selected_animals.location(+);
end your_proc_name;
/
If not, then please explain a bit more about the requirements you're trying to satisfy.

If condition with select query in oracle

How to Use IF Condition with Select Query in Oracle. Please suggest
IF (SELECT ptr_forecast_dt from ptr_details WHERE ptr_line_id = prmptr_line_id AND ptr_actual_dt IS NULL) IS NOT NULL THEN
SELECT ptr_forecast_dt INTO Forcast_dt from ptr_details WHERE ptr_line_id = prmptr_line_id AND ptr_actual_dt IS NULL;
END IF;
You can simply add another condition in the WHERE clause, as below:
SELECT ptr_forecast_dt
INTO Forcast_dt
FROM ptr_details
WHERE ptr_line_id = prmptr_line_id
AND ptr_actual_dt IS NULL
AND ptr_forecast_dt IS NOT NULL;
If you want to insert an alternate value if ptr_forecast_dt is NULL, then you can use the NVL function, as below:
SELECT NVL(ptr_forecast_dt, TO_DATE('01/01/2014', 'MM/DD/YYYY'))
INTO Forcast_dt
FROM ptr_details
WHERE ptr_line_id = prmptr_line_id
AND ptr_actual_dt IS NULL;

Oracle can I use scalar functions in WHERE Clause? or a NULL issue

somehow I get never any results when I call:
select * from table_1 t1 where t1.c1 IS NOT NULL and trim(t1.c1) != '' ;
trim(t1.c1) != '' part causes problems actually i return nothing.
Oracle is peculiar in that the empty string ('') and NULL are the same thing. It is as if you are saying:
trim(t1.c1) != NULL
Such a statement will never be true. Try:
trim(t1.c1) IS NOT NULL

Resources