Merge two tables by two variables in SQL Server -
i have 2 tables want merge 2 common variables.
so have table1
var1 var2 dates ---------- ---------- ---------------------- 111111 aa 1990-06-20 111111 aa 2000-06-20 222222 bb 1963-06-20 222222 bb 2005-06-20 333333 cc 2006-06-30 333333 cc 2016-06-26 and table2
var2 dates ---------- ---------------------- aa 1990-06-20 bb 1963-06-20 i want output be
var1 var2 dates ---------- ---------- ---------------------- 111111 aa 1990-06-20 222222 bb 1963-06-20 i have tried inner join 2 times using "var2" , "dates" in 2 tables. results gave me more rows (duplicate entries) need.
well, common way use inner join:
select t1.* table1 t1 inner join table2 t2 on t1.var1 = t2.var1 , t1.dates = t2.dates but makes more sense when want use columns second table. in case, just:
select * table1 t1 exists(select 1 table2 var1 = t1.var1 , dates = t1.dates)
Comments
Post a Comment