Sql 如果在表中发现重复数据(不是重复记录),则抛出错误

Sql 如果在表中发现重复数据(不是重复记录),则抛出错误,sql,oracle,oracle11g,Sql,Oracle,Oracle11g,我有一个包含产品信息的表,我需要检查重复记录并抛出错误。有两列,product key和product value,其中product value包含分号分隔值和正常值。下面给出了示例数据。(无限制) 现在我必须检查任意两行是否具有相同的product_键和product_标记值,如果product_键是xhmr,那么product_值应该被视为product_标记值。下面是我写的问题 select source_id, PRODUCT_KEY, rec , (case when instr

我有一个包含产品信息的表,我需要检查重复记录并抛出错误。有两列,product key和product value,其中product value包含分号分隔值和正常值。下面给出了示例数据。(无限制)

现在我必须检查任意两行是否具有相同的product_键和product_标记值,如果product_键是xhmr,那么product_值应该被视为product_标记值。下面是我写的问题


select source_id, PRODUCT_KEY, rec , (case when  instr(rec,'PRODUCT_TAG')<>0 THEN regexp_substr(TRIM(rec), '[^=]+', 1,2)
ELSE rec
end) as PRODUCT_TAG
from 
(select source_id,PRODUCT_KEY ,regexp_substr(TRIM(PRODUCT_VALUE), '[^;]+', 1,LEVEL) AS rec from products 
 connect by  regexp_substr(TRIM(PRODUCT_VALUE), '[^;]+', 1,LEVEL) is not null
    AND prior source_id = source_id
   AND PRIOR SYS_GUID() IS NOT NULL) where instr(rec,'PRODUCT_TAG')<>0 or PRODUCT_KEY in('xhmr');


在此之后,我将对所有行和所有不同的行进行计数。如果两个计数不相等,则抛出错误。我想知道,是否所有这些都可以用一种简洁的方式来完成。

我认为您可以执行以下操作

  • 获取主查询并放入with子句
  • 然后,您可以根据需要多次使用它
你可以这样写:

更新

  with main_query 
    as ( 
    select source_id, PRODUCT_KEY, rec , (case when  instr(rec,'PRODUCT_TAG')<>0 THEN regexp_substr(TRIM(rec), '[^=]+', 1,2)
    ELSE rec
    end) as PRODUCT_TAG
    from 
    (select source_id,PRODUCT_KEY ,regexp_substr(TRIM(PRODUCT_VALUE), '[^;]+', 1,LEVEL) AS rec from products 
     connect by  regexp_substr(TRIM(PRODUCT_VALUE), '[^;]+', 1,LEVEL) is not null
        AND prior source_id = source_id
       AND PRIOR SYS_GUID() IS NOT NULL) where instr(rec,'PRODUCT_TAG')<>0 or PRODUCT_KEY in('xhmr')
    ) 
    select 
    case when total_value = tot_dist then 'OK' -- whatever you want here
    else 'ERROR' -- whatever you want here
    end as result
    from 
    (  ( select count(*) as total_value  from main_query ) ,
       ( select count(*) as total_dist  from ( select distinct * from main_query ) ) 
    )

基于主选择的所有字段的不同行?yes@RobertoHernandez
和Previor SYS_GUID()不为NULL
没有任何意义。
sys\u guid()
的结果永远不会为
null
。你也可以忽略这个条件。如果没有这个条件,我将得到错误…在用户中通过循环连接data@a_horse_with_no_namecount(distinct(*)显示错误,我认为要找到多个列的distinct count,我们必须选择不同的行,然后对它们进行计数。请等一分钟;)你完全正确。在进行计数之前,我必须选择不同的值。我在原始答案中缺少了不同的值(选择计数xxx)。让我知道它是否有效now@AvinashTiwari,你能接受这个答案吗?真的很感激
source_id |Product_key | Product_value
-----------------------------------------------------------
     1      xzy           SCENT
     1      xhmr          POWDER
     1      abc           COMB
     1      xhmr          OIL

  with main_query 
    as ( 
    select source_id, PRODUCT_KEY, rec , (case when  instr(rec,'PRODUCT_TAG')<>0 THEN regexp_substr(TRIM(rec), '[^=]+', 1,2)
    ELSE rec
    end) as PRODUCT_TAG
    from 
    (select source_id,PRODUCT_KEY ,regexp_substr(TRIM(PRODUCT_VALUE), '[^;]+', 1,LEVEL) AS rec from products 
     connect by  regexp_substr(TRIM(PRODUCT_VALUE), '[^;]+', 1,LEVEL) is not null
        AND prior source_id = source_id
       AND PRIOR SYS_GUID() IS NOT NULL) where instr(rec,'PRODUCT_TAG')<>0 or PRODUCT_KEY in('xhmr')
    ) 
    select 
    case when total_value = tot_dist then 'OK' -- whatever you want here
    else 'ERROR' -- whatever you want here
    end as result
    from 
    (  ( select count(*) as total_value  from main_query ) ,
       ( select count(*) as total_dist  from ( select distinct * from main_query ) ) 
    )
 SQL> desc my_test
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 C1                                                 NUMBER
 C2                                                 NUMBER

SQL> select * from my_test ;

        C1         C2
---------- ----------
         1          1
         1          1
         2          2

SQL>  with main_query as ( select c1 , c2 from my_test )
  select a , b
 from
 ( select count(*) as a  from main_query ) ,
 ( select count(*) as b  from ( select distinct * from main_query ) )
 /  2    3    4    5    6

         A          B
---------- ----------
         3          2

 SQL>  with main_query as ( select c1 , c2 from my_test )
  select case when a = b then 'OK' else 'ERROR' end as result
 from
 ( select count(*) as a  from main_query ) ,
 ( select count(*) as b  from ( select distinct * from main_query ) )
/

RESUL
-----
ERROR