SQL查询比较不匹配记录

时间:2019-02-20 02:00:19

标签: sql sql-server tsql

我需要比较英国的美国记录。

条形码和仓库字段上的英国记录是英国要遵循或复制的模式。

在设置了每个英国的项目组的英国记录后,我想通过写一个查询来检查哪些项目组是与美国不匹配的记录

请参见下面的样本表插图。

Country Item Group  Barcode     Warehouse
------------------------------------------
UK      Clothes     Standard    A
USA     Clothes     Standard    B
UK      Gift        Standard    A
USA     Gift        Standard    A
UK      Shoes       Standard    A
USA     Shoes       EAN         B

我编写了这段代码,但是它列出了所有记录,并且我需要手动检查哪些记录不匹配(如果有成千上万条记录,将更加困难)。

 SELECT 
     a.country, b.category, a.barcode, a.warehouse
 FROM 
     Retail a 
 INNER JOIN 
     Category b on a.ID = b.category
 WHERE 
     a.country IN ('USA', 'UK')
 ORDER BY 
     b.category, 

我想通过编写仅显示不匹配记录的SQL查询来对此进行修改。预期结果集输出应为

Country Item Group  Barcode     Warehouse
-----------------------------------------
UK      Clothes     Standard    A
USA     Clothes     Standard    B
UK      Shoes       Standard    A
USA     Shoes       EAN         B

由于衣服项目组(仓库)不匹配,鞋子也(条形码,仓库)不匹配

4 个答案:

答案 0 :(得分:4)

您可以尝试在子查询中使用COUNT窗口函数

SELECT *
FROM (
    SELECT a.country, 
           b.category, 
           a.barcode, 
           a.warehouse,
           COUNT(*) OVER(PARTITION BY b.category,a.Warehouse ORDER BY category) cnt
    FROM Retail a INNER JOIN Category b on a.ID = b.category
    WHERE a.country in ('USA', 'UK')
) t1
WHERE t1.cnt = 1

答案 1 :(得分:2)

这是您想要的吗?

select r.*
from retail r
where (r.country ='USA' and
       exists (select 1
               from retail r2
               where r2.country = 'UK' and
                     r2.itemgroup = r.itemgroup and
                     (r2.barcode <> r.barcode or
                      r2.warehouse <> r.warehouse
                     )
              )
       ) or
       (r.country ='UK' and
       exists (select 1
               from retail r2
               where r2.country = 'USA' and
                     r2.itemgroup = r.itemgroup and
                     (r2.barcode <> r.barcode or
                      r2.warehouse <> r.warehouse
                     )
              )
       ) ;

答案 2 :(得分:0)

    with d as 
    (
    SELECT a.country, b.category, a.barcode, a.warehouse
     FROM Retail a INNER JOIN Category b on a.ID = b.category
     WHERE a.country in ('USA', 'UK')
    )
    select d1.* from d as d1 inner join
    (
    select a.country,  category
    from d
    group by
    a.country, category
    having count(distinct barcode)>1 or count(distinct warehouse)>1
    ) d2
    on d1.country=d2.country and d1.category=d2.category
    order by
    d1.country=d2.country

答案 3 :(得分:0)

WITH data AS (
    SELECT a.country, b.category, a.barcode, a.warehouse
    FROM Retail a INNER JOIN Category b 
        ON a.ID = b.category
    WHERE a.country IN('USA', 'UK')
)

SELECT T1.* 
FROM data AS T1
INNER JOIN data AS T2
    ON T1.ITEM_GROUP = T2.ITEM_GROUP
    AND (T1.BARCODE   <> T2.BARCODE 
      OR T1.WAREHOUSE <> T2.WAREHOUSE)
WHERE (T1.COUNTRY = 'UK'  AND T2.COUNTRY = 'USA')
   OR (T1.COUNTRY = 'USA' AND T2.COUNTRY = 'UK' )
;