简体   繁体   中英

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. I need help writing a query that will pull all the values that are Active in US but inactive or nonexistant in CA. So for the example above the valeus CCCCC and DDDDD should be pulled. 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.

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. Perhaps if you structure your query as a CTE the solution will become more obvious to you?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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