简体   繁体   English

如何比较多个表中的记录,但查找不同或缺失的值

[英]How do you compare records within the multiple tables but look for different or missing values

Here is a version of my Table: 这是我的桌子的一个版本:

p.productid | pp.productid|  pp.productpricingid | p.erpsku | pp.countrycode | pp.productstatus
 1            1                    1             AAAAA        US             Active
 2            2                    2             BBBBB        US             Active
 3            3                    3             CCCCC        US             Active
 4            4                    4             DDDDD        US             Active

 1            1                    5             AAAAA        CA             Active
 2            2                    6             BBBBB        CA             Active
 3            3                    7             CCCCC        CA             InActive

So I have 2 tables products, and product pricing. 所以我有2表产品,以及产品定价。 I need help writing a query that will pull all the values that are Active in US but inactive or nonexistant in CA. 我需要编写查询的帮助,该查询将提取在美国处于活动状态但在CA中处于非活动状态或不存在的所有值。 So for the example above the valeus CCCCC and DDDDD should be pulled. 因此,对于上面的示例,应将valeus CCCCC和DDDDD拉出。 The tricky thing is that all this data lives in both the product and productpricing tables, which I combine to make one data set. 棘手的是,所有这些数据都存在于产品表和产品定价表中,我将它们合并成一个数据集。 Then within that dataset I have to write another nested query where I find all the differences. 然后,在该数据集中,我必须编写另一个嵌套查询,在其中找到所有差异。 Right now I can find all the values that are not in the US table, but Im not sure if this query is accurate. 现在,我可以找到不在US表中的所有值,但是我不确定此查询是否正确。

Here is a sample of my Query: 这是我的查询示例:

select p.erpsku from

product p, productpricing pp

where

p.productid = pp.productid

and pp.countrycode = 'us'

and pp.productstatus = 'Active'

and p.productid not in (select p.productid from product p, productpricing pp where p.productid = pp. productid and 

pp.countrycode = 'CA' )

However I am unsure if this query is accurate at all. 但是我不确定此查询是否完全正确。 I would appreciate any help I could get with this. 我将不胜感激。

If you include Danny's suggestion you've got it right. 如果您包括丹尼(Danny)的建议,那么您做对了。 Perhaps if you structure your query as a CTE the solution will become more obvious to you? 也许如果您将查询构造为CTE ,解决方案对您来说将变得更加明显?

create table #DataTable (
     erpsku varchar(20)
    ,countrycode char(2)
    ,productstatus varchar(8)
);


insert #DataTable
values
     ('AAAAA', 'US', 'Active')
    ,('BBBBB', 'US', 'Active')
    ,('CCCCC', 'US', 'Active')
    ,('DDDDD', 'US', 'Active');

-- All regions' data in one table
insert #DataTable
values
     ('AAAAA', 'CA', 'Active')
    ,('BBBBB', 'CA', 'Active')
    ,('CCCCC', 'CA', 'InActive');


;with US_Active as
(
    select
        erpsku
    from #DataTable
    where countrycode = 'US'
    and productstatus = 'Active'
)
, CA_Active as
(
    select
        erpsku
    from #DataTable
    where countrycode = 'CA'
    and productstatus = 'Active'
)
, US_Not_CA as
(
    select
        erpsku
    from US_Active
    EXCEPT
    select
        erpsku
    from CA_Active
)
select * from US_Not_CA;

I've abbreviated your sample table for simplicity. 为了简单起见,我将您的示例表缩写为。 What I've done with erpsku could be done just as well with ProdustID. 我用erpsku做的事情也可以用ProdustID完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM