在Oracle中,如何使用过程处理以下情况?

时间:2014-12-04 04:09:45

标签: oracle procedure

假设此表中包含以下数据。

123 || MYNTRA || 345 || Flipkart
123 || MYNTRA || 678 || Snapdeal

In Table 1, 123 is PARENT whose Children are 345,678. There are 'N' no. of such Children to Parent 123.

Now Assume that 123 has following 2 offers i.e Parent has 3 offers
123 || Offer1_Myntra || Description_Offer1
123 || Offer2_Myntra || Description_Offer2

Now as Children are extended from Parent, Parent's Each offer SHOULD be in Children with SAME DATA (Not Even Mismatch of single space)
i.e 

345 || Offer1_Myntra || Description_Offer1
678 || Offer2_Myntra || Description_Offer2

Also Order of occurrence of offers in Children may vary like as : ()

678 || Offer2_Myntra || Description_Offer2
345 || Offer1_Myntra || Description_Offer1

我想使用光标和放大器来比较这些情况。 for循环。 我想在这个数据和数据之间找出MISMATCH。显示具有MISMATCH的列。 你能帮忙解决逻辑问题吗?代码?

1 个答案:

答案 0 :(得分:0)

我会建议某种方式,只能使用下面的SQL查询

首先我将命名列。 表1列

parent_1
site_1
child_1
site_2

table2,其中包含以下列

parent_1 
offer_1
off_desc_1

现在代码将是

create table tmp_tab(parent_1 varchar2(50),child_1 varchar2(50),offer_1 varchar2(50),off_desc_1 varchar2(50));

Insert into tmp_tab
select a.parent_1,a.child_1,b.offer_1,b.off_desc_1 
from table1 a
join 
table2 b
on a.parent_1 = b.paren_1

将给出parent_1之间的交叉连接,因此结果将是

select * from tmp_tab;

123||345 || Offer1_Myntra || Description_Offer1
123||678 || Offer2_Myntra || Description_Offer2
123||678 || Offer2_Myntra || Description_Offer2
123||345 || Offer1_Myntra || Description_Offer1

从上面我们可以很容易地消除下面的重复

delete from 
   tmp_tab a
where 
   a.rowid > 
   any (select b.rowid
   from 
      tmp_tab b
   where 
      a.parent_1 = b.parent_1
   and 
      a.child_1 = b.child_1
   )
; 

会给你预期的结果