分层查询中树的比较

时间:2013-04-17 15:36:15

标签: oracle

我需要以下面的方式提取数据

     Compare_Result          Association_Type_name     Association_Role 

Type in Dev,but not Prod      ProcessingSite           Null
Role in Dev,but not Prod      ProcessingSite           MoodedActivity
Role in Dev,but not Prod      Specimen                 ProductRelationship

要求:我需要比较开发和生产。如果生产中不存在任何association_type或association_role,那么我需要返回一条消息,如“在开发中找到的类型对象,但不是生产”

任何人都可以给我一个想法。

示例数据

 create table nodes
 (
 node_id number(5),
 parent_node_id number(5),
 object_id number(5)
 );

 begin
 insert into nodes values(4001,7001,2001);
 insert into nodes values(4002,4001,2005);
 insert into nodes values(4003,4002,2002);
 insert into nodes values(4004,4003,2004);
 insert into nodes values(4005,4004,2003);
 insert into nodes values(4006,4004,2006);
 insert into nodes values(4007,4004,2007);
 insert into nodes values(4008,4003,2008);
 insert into nodes values(4009,4008,2011);
 insert into nodes values(4010,4008,2013);
 insert into nodes values(4011,4008,2014);
 insert into nodes values(4012,4003,2009);
 insert into nodes values(4013,4012,2015);
 insert into nodes values(4014,4012,2017);
 insert into nodes values(4015,4001,2020);
 insert into nodes values(4016,4015,2012);
 insert into nodes values(4017,4016,2016);
 insert into nodes values(4018,4017,2024);
 insert into nodes values(4019,4017,2023);
 insert into nodes values(4020,4016,2029);
 insert into nodes values(4021,4020,2022);
 insert into nodes values(4022,4020,2021);
 insert into nodes values(4023,4020,2019);
 insert into nodes values(4024,4016,2010);
 insert into nodes values(4025,4024,2018);
 end;
 /

 create table objects
 (
 object_id number(5),
 object_type_id number(5),
 name varchar2(40)
 );

 begin
 insert into objects values ( 2001,5001,'iad_Dictionary_area');
 insert into objects values ( 2002,5003,'iad_Association_Dictionary');
 insert into objects values ( 2003,5005,'TreatingSite');
 insert into objects values ( 2004,5004,'ProcessingSite');
 insert into objects values ( 2005,5002,'Development');
 insert into objects values ( 2006,5005,'MoodedActivity');
 insert into objects values ( 2007,5005,'MaterialName');
 insert into objects values ( 2008,5004,'PerformedClass');
 insert into objects values ( 2009,5004,'Specimen');
 insert into objects values ( 2010,5004,'Specimen');
 insert into objects values ( 2011,5005,'RegulatoryAssessment');
 insert into objects values ( 2012,5003,'iad_Association_Dictionary');
 insert into objects values ( 2013,5005,'Submission');
 insert into objects values ( 2014,5005,'Mooded');
 insert into objects values ( 2015,5005,'class13th');
 insert into objects values ( 2016,5004,'ProcessingSite');
 insert into objects values ( 2017,5005,'ProductRelationship');
 insert into objects values ( 2018,5005,'class13th');
 insert into objects values ( 2019,5005,'Distributor');
 insert into objects values ( 2020,5006,'Production');
 insert into objects values ( 2021,5005,'Assessor');
 insert into objects values ( 2022,5005,'StudyInvestigator');
 insert into objects values ( 2023,5005,'MaterialName');
 insert into objects values ( 2024,5005,'TreatingSite');
 insert into objects values ( 2029,5004,'BiologicEntityGroup');
 end;
 /

 create table object_types
 (
 object_type_id number(5),
 name varchar2(35)
 );

 begin
 insert into object_types values (5001,'Dictionary_area');
 insert into object_types values (5002,'Development');
 insert into object_types values (5003,'Association_Dictionary');
 insert into object_types values (5004,'Association_Type');
 insert into object_types values (5005,'Association_Role');
 insert into object_types values (5006,'Production');
 end;
 /

例如

让我们假设我有以下结构

  Dictionary_area
      development
          Association_Dictionary 
                ProcessingSite(type1) 
                    TreatingSite(role1)
                    MoodedActivity(role2)
                    MaterialName(role3)


                PerformedClass(type2)
                    RegulatoryAssessment(role1)
                    Submission(role2)
                    Mooded(role3)

                Specimen(type3)
                    class13th(role1)
                    ProductRelationship(role2)      

       production
          Association_Dictionary
                ProcessingSite(type1)
                    TreatingSite(role1)
                    MaterialName(role2)

                BiologicEntityGroup(type2)
                    StudyInvestigator(role1)
                    Assessor(role2)
                    Distributor(role3) 

                Specimen (type3)      
                    class13th(role1)

ProcessingSite是一个association_type,它有3个association_roles。我们可以在生产中观察到相同的association_type,但是MoodedActivity(association_role)不可用。在这种情况下,我们需要返回“在开发中找到的类型对象,而不是生产”。

表格信息

节点:此表将具有节点和父节点信息。例如,如果开发对象的某个节点为100,其父节点为99。

对象:对象信息。

object_types:object_type信息。例如,ProcessingSite object_type是“Association_Type”。

我尝试了以下方式

 WITH childs AS
 (SELECT n.object_id, n.node_id, n.parent_node_id, ot.NAME, "path",
         rootnode
    FROM (SELECT     object_id, node_id, parent_node_id,
                     SYS_CONNECT_BY_PATH (object_id, '/') "path",
                     CONNECT_BY_ROOT (object_id) rootnode
                FROM nodes

          START WITH object_id = 2001
          CONNECT BY PRIOR node_id = parent_node_id) n
         JOIN
         objects o ON n.object_id = o.object_id
         JOIN object_types ot ON o.object_type_id = ot.object_type_id
         ),
 devobj AS
 (SELECT n.object_id, n.NAME
    FROM (SELECT *
            FROM childs) n
   WHERE n.NAME = 'Development'),
 prodobj AS
 (SELECT n.object_id, n.NAME
    FROM (SELECT *
            FROM childs) n
   WHERE n.NAME = 'Production'),
 dev_associ_type_obj AS
 (SELECT *
    FROM (SELECT     object_id, NAME
                FROM (SELECT *
                        FROM childs)
          START WITH object_id = (SELECT object_id
                                    FROM devobj)
          CONNECT BY PRIOR node_id = parent_node_id) t
   WHERE t.NAME = 'Association_Type'),
 dev_associ_roles_obj AS
 (SELECT     object_id, NAME, CONNECT_BY_ROOT (object_id) rootnode
        FROM (SELECT *
                FROM childs)
  START WITH object_id IN (SELECT object_id
                             FROM dev_associ_type_obj)
  CONNECT BY PRIOR node_id = parent_node_id),
 prod_associ_type_obj AS
 (SELECT *
    FROM (SELECT     object_id, NAME
                FROM (SELECT *
                        FROM childs)
          START WITH object_id = (SELECT object_id
                                    FROM devobj)
          CONNECT BY PRIOR node_id = parent_node_id) t
   WHERE t.NAME = 'Association_Type'),
 prod_associ_roles_obj AS
 (SELECT     object_id, NAME, CONNECT_BY_ROOT (object_id) rootnode
        FROM (SELECT *
                FROM childs)
  START WITH object_id IN (SELECT object_id
                             FROM prod_associ_type_obj)
  CONNECT BY PRIOR node_id = parent_node_id)
  SELECT *
  FROM prod_associ_roles_obj
  MINUS
  SELECT *
  FROM dev_associ_roles_obj

0 个答案:

没有答案