SQL Statement for Reconciliation

爱⌒轻易说出口 提交于 2019-12-05 19:56:06

how about this:

  INSERT INTO TBL_RESULT (ID, TBL1_ID, TBL2_ID) 
  SELECT seq_tbl_result.nextval,t1.id,t2.id 
  FROM
  (SELECT t1.match_criteria,t1.id, row_number() OVER (PARTITION BY t1.match_criteria ORDER BY t1.id) rn 
   FROM tbl1 t1) t1,  
  (SELECT t2.match_criteria,t2.id, row_number() OVER (PARTITION BY t2.match_criteria ORDER BY t2.id) rn 
   FROM tbl2 t2) t2
  WHERE t1.match_criteria=t2.match_criteria AND t1.rn=t2.rn

Note: It assumes that there are an equal number of rows in each matching set in both tables.

What about something like this:

insert into TBL_RESULT (ID, TBL1_ID, TBL2_ID)
select SEQ_TBL_RESULT.nextval, TBL1.ID, TBL2.ID 
from TBl1, TBL2
where TBL1.MATCH_CRITERIA = TBL2.MATCH_CRITERIA
AND (NOT EXISTS (SELECT 1 FROM TBL_RESULT WHERE TBL1_ID = TBL1.ID) OR NOT EXISTS(SELECT 1 FROM TBL_RESULT WHERE TBL2_ID = TBL2.id))

This should prevent violation of your unique constraints.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!