SQL Server: How to select missing rows in table from another table? -


i have 2 tables below:

table1:

storeid sku ------------ 1       abc 2       abc 3       abc 1       xyz 4       xyz 

table2:

 storeid  --------     1     2     3     4     5 

i want select missing storeid table1 in table 2. condition in above example sku abc storeid 4 , 5 missing , sku xyz 2,3,5 missing. want below table output

sku,id ------ abc 4 abc 5 xyz 2 xyz 3 xyz 5 

i able pull overall missing store 5 using below query.

 select       sku, t2.storeid         @table1 t1   full outer join      @table2 t2 on t1.storeid = t2.storeid        t1.storeid null 

below test create , insert query.

declare @table1 table (  storeid varchar(4),  sku varchar(5) )  declare @table2 table (  storeid int ) begin insert @table1(sku,storeid) values('abc',1) insert @table1(sku,storeid) values('abc',2) insert @table1(sku,storeid) values('abc',3) insert @table1(sku,storeid) values('xyz',1) insert @table1(sku,storeid) values('xyz',4) insert @table2(storeid) values(1) insert @table2(storeid) values(2) insert @table2(storeid) values(3) insert @table2(storeid) values(4) insert @table2(storeid) values(5) end 

thank you

you need list of skus , tables, , show rows not appear in table1:

select sku, storeid @table2 t2 cross join (select distinct sku @table1) t1 not exists (select 1 @table1 table1                    table1.sku = t1.sku                      , table1.storeid = t2.storeid) 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -