PL / SQL通过临时表比较数据集

时间:2014-09-11 15:16:11

标签: sql plsql

我设法创建了两个临时表,用于比较两个数据集,即我们将有两个临时表(dataset1和dataset2),然后检查特定列以查找相似性/差异。

我遇到的主要问题是完全清除临时表。我希望每次都有一个新的临时表,其中包含特定于我的初始select语句的数据。

Create global temporary table dataset_1 
ON COMMIT PRESERVE ROWS
as
select  
 ar.action_sequence as "ACTION_SEQUENCE",
 ar.action_id as "ACTION",
 ar.action_outcome_id as "OUTCOME",
 case when ar.result_action_id is null then 909 else ar.result_action_id end as "RESULT_ACTION"

from 
 ( 
      select
          distinct  ar.action_id ,
           ar.result_action_id, 
           ar.action_outcome_id,
           level action_sequence
      from
           action_result ar
      start with
           ar.action_id = 969726 --&WIP  
      connect by nocycle prior ar.result_action_id = ar.action_id and level < 25
 )  ar ,
 action aca, 
 action acr, 
 outcome oc,
 object_type obj
where
 ar.action_id = aca.action_id
and      
 nvl(ar.result_action_id,ar.action_id ) = acr.action_id
and
 ar.action_outcome_id = oc.outcome_id
and 
 aca.object_type_id = obj.object_type_id

order by 
 ar.action_sequence, aca.action_name;

“datset2”临时表的构建方式与此代码相同。然后我运行此查询以找到我的交叉点:

select action_sequence, result_action from dataset_1 where result_action in (select action from dataset_2);

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

BEGIN
  EXECUTE IMMEDIATE 'TRUNCATE TABLE DATASET1';
  EXECUTE IMMEDIATE 'TRUNCATE TABLE DATASET2';

  -- logic to load temp tables with fresh data goes here

  -- logic to compare tables goes here
END;

分享并享受。

答案 1 :(得分:0)

BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE dataset_1';
EXECUTE IMMEDIATE 'TRUNCATE TABLE dataset_2';
insert into dataset_1 
select  
 ar.action_sequence as "ACTION_SEQUENCE",
 ar.action_id as "ACTION",
 ar.action_outcome_id as "OUTCOME",
 case when ar.result_action_id is null then 909 else ar.result_action_id end as "RESULT_ACTION"

from 
 ( 
      select
          distinct  ar.action_id ,
           ar.result_action_id, 
           ar.action_outcome_id,
           level action_sequence
      from
           action_result ar
      start with
           ar.action_id = &WIP  
      connect by nocycle prior ar.result_action_id = ar.action_id and level < 25
 )  ar ,
 action aca, 
 action acr, 
 outcome oc,
 object_type obj
where
 ar.action_id = aca.action_id
and      
 nvl(ar.result_action_id,ar.action_id ) = acr.action_id
and
 ar.action_outcome_id = oc.outcome_id
and 
 aca.object_type_id = obj.object_type_id

order by 
 ar.action_sequence, aca.action_name;

---WIP Dataset

insert into dataset_2 
select  
 ar.action_sequence as "ACTION_SEQUENCE",
 ar.action_id as "ACTION",
 ar.action_outcome_id as "OUTCOME",
 case when ar.result_action_id is null then 909 else ar.result_action_id end as "RESULT_ACTION"

from 
 ( 
      select
          distinct  ar.action_id ,
           ar.result_action_id, 
           ar.action_outcome_id,
           level action_sequence
      from
           action_result ar
      start with
           ar.action_id = &Built  
      connect by nocycle prior ar.result_action_id = ar.action_id and level < 25
 )  ar ,
 action aca, 
 action acr, 
 outcome oc,
 object_type obj
where
 ar.action_id = aca.action_id
and      
 nvl(ar.result_action_id,ar.action_id ) = acr.action_id
and
 ar.action_outcome_id = oc.outcome_id
and 
 aca.object_type_id = obj.object_type_id

order by 
 ar.action_sequence, aca.action_name;

END;

感谢您的帮助!它正在运行,这是最终的代码!