我有三张桌子;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
最后d_algo_maj
:
NUM_MISE_A_JOUR ID_ALGO VERSION_ALGO
--------------- ---------- ------------
11 301 A301V1
我想创建一个查询,给出如下结果:
id_algo | version_algo | has_maj
300 | A300V1 | 0
301 | A301V1 | 1
302 | A302V1 | 1
其中前两列来自d_algorithme
,并且has_maj
是 0 或 1,具体取决于在 中是否存在直接或间接引用的算法d_algo_maj
。间接引用是通过一个或多个d_algo_ope
记录,它们一起形成一个层次结构。
对于显示的示例数据:
- 300:没有算法或
d_algo_ope
记录,id_algo = 1
也没有d_algo_maj
记录id_algo = 300
。 - 301:有一条
d_algo_maj
记录id_algo = 301
(足以使has_maj
列设置为1)。 - 302:没有
d_algo_maj
记录id_algo = 302
。但是有一个d_algo_ope
记录,num_operation = 301
它id_algo = 302
意味着 302 算法引用 301 算法(它有 amaj
),因此该has_maj
列应设置为 1。
这是 DDL 和 DML 以及其他详细信息(根据我的实际情况进行了简化):
-- 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');