Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何比较多个表中的记录,但查找不同或缺少的值_Sql - Fatal编程技术网

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

Sql 如何比较多个表中的记录,但查找不同或缺少的值,sql,Sql,这是我的表格的一个版本: p.productid | pp.productid| pp.productpricingid | p.erpsku | pp.countrycode | pp.productstatus 1 1 1 AAAAA US Active 2 2 2 BBBBB

这是我的表格的一个版本:

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
所以我有两张表,产品和产品定价。我需要帮助编写一个查询,该查询将提取所有在美国活动但在CA不活动或不存在的值。因此,对于上面的示例,应提取valeus CCCCC和DDD。棘手的是,所有这些数据都存在于product和productpricing表中,我将它们组合成一个数据集。然后,在该数据集中,我必须编写另一个嵌套查询,在其中找到所有差异。现在我可以找到所有不在US表中的值,但是我不确定这个查询是否准确

以下是我的查询示例:

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' )

但是我不确定这个查询是否准确。如果能得到任何帮助,我将不胜感激。

如果你把丹尼的建议包括在内,那你就做对了。也许如果您将查询构造为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;

为了简单起见,我简化了你的样品表。我对erpsku所做的工作也可以用ProdustID来完成。

在CA子选项中,如果您可以记录countrycode为“CA”,productstatus为“InActive”,则您需要确保只查找活动状态。Google Hi Danny,我将子选项中的状态更改为InActive,但是,返回值是存在于美国列表中但不存在于CA列表中的所有SKU的值。我需要找到SKU在美国列表中存在且处于活动状态,但在CA列表中存在且处于非活动状态的值。这有意义吗?@user3512974-是的,但是您希望子选择检查“活动”,因为您正在检查p.productd不在子选择中的位置。