0

我有三张桌子;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 = 301id_algo = 302意味着 302 算法引用 301 算法(它有 a maj),因此该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');
4

1 回答 1

1

如果我了解您在做什么以及表之间的链接,那么我认为您可以通过递归子查询分解获得所需的结果(假设您使用的是 11gR2 或更高版本):

with r (id_algo, version_algo, has_maj, last_id_algo, last_version_algo) as (
  select da.id_algo, da.version_algo, decode(dm.id_algo, null, 0, 1),
    da.id_algo, da.version_algo
  from d_algorithme da
  left join d_algo_maj dm
  on dm.id_algo = da.id_algo
  and dm.version_algo = da.version_algo
  union all
  select dao.id_algo, dao.version_algo, decode(dm.id_algo, null, 0, 1),
    dao.id_algo, dao.version_algo
  from r
  join d_algo_ope dao
  on dao.id_algo = r.last_id_algo
  and dao.version_algo = r.last_version_algo
  left join d_algo_maj dm
  on dm.id_algo = dao.num_operation
)
cycle id_algo, version_algo set is_cycle  to 1 default 0
select id_algo, version_algo, max(has_maj) as has_maj
from r
group by id_algo, version_algo
order by id_algo, version_algo;

   ID_ALGO VERSION_ALGO    HAS_MAJ
---------- ------------ ----------
       300 A300V1                0
       301 A301V1                1
       302 A302V1                1

rCTE 有一个锚成员,它将行外连接到d_algorithmed_algo_maj并使用 decode 在该级别生成一个标志,或者为零或一。该部分自行运行将得到:

   ID_ALGO VERSION_ALGO    HAS_MAJ LAST_ID_ALGO LAST_VERSION_ALGO
---------- ------------ ---------- ------------ -----------------
       300 A300V1                0          300 A300V1           
       301 A301V1                1          301 A301V1           
       302 A302V1                0          302 A302V1           

然后递归成员查找任何匹配的记录,并以相同的方式将d_aldo_ope其外连接d_algo_maj得到相同的标志。该部分本身将获得:

   ID_ALGO VERSION_ALGO    HAS_MAJ LAST_ID_ALGO LAST_VERSION_ALGO
---------- ------------ ---------- ------------ -----------------
       300 A300V1                0          300 A300V1           
       301 A301V1                0          301 A301V1           
       302 A302V1                1          302 A302V1           

但是,如果您的级别比示例数据中显示的更多,则递归。

通过查找每个 ID/版本的聚合将它们结合起来max(has_maj)意味着任何级别的匹配主要记录的总体标志值为 1,如果根本没有匹配项,您只会得到 0 - 只有 ID 300 与此数据发生这种情况.

于 2015-05-11T17:50:58.013 回答