I have three tables; d_algorithme
:
ID_ALGO VERSION_ALGO LIBELLE_ALGO
---------- ------------ --------------------------------------------------
300 A300V1 Algo_300_V1
301 A301V1 Algo_301_V1
302 A302V1 Algo_302_V1
d_algo_ope
:
NUM_OPERATION ID_ALGO VERSION_ALGO TYP_OPERATION NUM_ORDRE
------------- ---------- ------------ ------------- ----------
300 301 A301V1 3 1
1 300 A300V1 3 1
301 302 A302V1 3 1
and finally d_algo_maj
:
NUM_MISE_A_JOUR ID_ALGO VERSION_ALGO
--------------- ---------- ------------
11 301 A301V1
I want to create a query giving a result like :
id_algo | version_algo | has_maj
300 | A300V1 | 0
301 | A301V1 | 1
302 | A302V1 | 1
Where the first two columns are from d_algorithme
, and has_maj
is 0 or 1 depending on whether there is an algorithm referenced directly or indirectly in d_algo_maj
. An indirect reference is via one or more d_algo_ope
records, which together form a hierarchy.
For the sample data shown:
- 300: there is no algorithm or
d_algo_ope
record withid_algo = 1
and there is nod_algo_maj
record withid_algo = 300
. - 301: there is a
d_algo_maj
record withid_algo = 301
(enough to makehas_maj
column be set to 1). - 302: there is no
d_algo_maj
record withid_algo = 302
. But there is ad_algo_ope
record withnum_operation = 301
and withid_algo = 302
which means that 302 algorithm references 301 algorithm (which has amaj
) and hence thehas_maj
column should be set to 1.
Here is DDL and DML and other details (simplified from what I have in reality):
-- DDL -----------------------------
-- d_algorithme
CREATE TABLE D_ALGORITHME
(
ID_ALGO NUMBER(10, 0) NOT NULL
, VERSION_ALGO VARCHAR2(6 BYTE) NOT NULL
, LIBELLE_ALGO VARCHAR2(50 BYTE) NOT NULL
) ;
ALTER TABLE D_ALGORITHME
ADD CONSTRAINT IX_D_ALGORITHME PRIMARY KEY
(
ID_ALGO
, VERSION_ALGO
);
-- d_algo_ope
CREATE TABLE D_ALGO_OPE
(
NUM_OPERATION NUMBER(10, 0) NOT NULL
, ID_ALGO NUMBER(10, 0) NOT NULL
, VERSION_ALGO VARCHAR2(6 BYTE) NOT NULL
, TYP_OPERATION NUMBER(6, 0) NOT NULL
, NUM_ORDRE NUMBER(10, 0) NOT NULL
);
ALTER TABLE D_ALGO_OPE
ADD CONSTRAINT IX_D_ALGO_OPE PRIMARY KEY
(
ID_ALGO
, VERSION_ALGO
, NUM_ORDRE
) ;
-- d_algo_maj
CREATE TABLE D_ALGO_MAJ
(
NUM_MISE_A_JOUR NUMBER(10, 0) NOT NULL
, ID_ALGO NUMBER(10, 0) NOT NULL
, VERSION_ALGO VARCHAR2(6 BYTE) NOT NULL
)
;
ALTER TABLE D_ALGO_MAJ
ADD CONSTRAINT IX_D_ALGO_MAJ PRIMARY KEY
(
ID_ALGO
, VERSION_ALGO
, NUM_MISE_A_JOUR
)
;
-- DML ----------------
REM INSERTING into D_ALGORITHME
Insert into D_ALGORITHME (ID_ALGO,VERSION_ALGO,LIBELLE_ALGO)
values ('300','A300V1','Algo_300_V1');
Insert into D_ALGORITHME (ID_ALGO,VERSION_ALGO,LIBELLE_ALGO)
values ('301','A301V1','Algo_301_V1');
Insert into D_ALGORITHME (ID_ALGO,VERSION_ALGO,LIBELLE_ALGO)
values ('302','A302V1','Algo_302_V1');
REM INSERTING into D_ALGO_OPE
Insert into D_ALGO_OPE
(NUM_OPERATION,ID_ALGO,VERSION_ALGO,TYP_OPERATION,NUM_ORDRE)
values ('300','301','A301V1','3','1');
Insert into D_ALGO_OPE (NUM_OPERATION,ID_ALGO,VERSION_ALGO,TYP_OPERATION,NUM_ORDRE)
values ('1','300','A300V1','3','1');
Insert into D_ALGO_OPE (NUM_OPERATION,ID_ALGO,VERSION_ALGO,TYP_OPERATION,NUM_ORDRE)
values ('301','302','A302V1','3','1');
REM INSERTING into D_ALGO_MAJ
Insert into D_ALGO_MAJ (NUM_MISE_A_JOUR,ID_ALGO,VERSION_ALGO)
values ('11','301','A301V1');