sql server - See if multiple entries exist in another table -


data set 1:

cust_ref    |   acc1    |    acc2   |    acc3 ------------+-----------+-----------+--------- 1000001     |   alpha   |   bravo   |   charlie 1000002     |   alpha   |   bravo   |   charlie 1000003     |   alpha   |   bravo   |   charlie 1000004     |   delta   |   echo    |    1000005     |   delta   |   echo    |    1000006     |   foxtrot |  1000007     |   foxtrot | 

data set 2:

cust_ref    |   acc ------------+-------- 1000001     |   alpha 1000001     |   bravo 1000001     |   delta 1000004     |   delta 1000004     |   echo 1000006     |   foxtrot 

data set 1 shows customer references , accounts should have. example 1000001 must have accounts - alpha, bravo, charlie. customer 1000002 has delta , echo, etc.

data set 2 shows accounts associated customer reference.

is there can return instances of missing accounts t-sql?

example: in dataset have provided customer 1000001 should have alpha, bravo, charlie data set 2 shows customer not have charlie.

considering ddl , sample data:

declare @table1 table (     cust_ref varchar(10) primary key,     acc1 varchar(10) null,     acc2 varchar(10) null,     acc3 varchar(10) null )  insert @table1 values ('1000001','alpha','bravo','charlie'), ('1000002','alpha','bravo','charlie'), ('1000003','alpha','bravo','charlie'), ('1000004','delta','echo',null), ('1000005','delta','echo',null), ('1000006','foxtrot','foxtrot',null), ('1000007','foxtrot','foxtrot',null)  declare @table2 table (     cust_ref varchar(10) not null,     acc varchar(10) not null )  insert @table2 values ('1000001','alpha'), ('1000001','bravo'), ('1000001','delta'), ('1000004','delta'), ('1000004','echo'), ('1000006','foxtrot') 

you use unpivot , except, way:

select cust_ref, acc @table1 unpivot (acc col in (acc1, acc2, acc3)) u except select cust_ref, acc @table2 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -