将表与sql Server 2012中的sql/tsql进行比较
SQL Server 2012中有四个表:表1、表2、表3和结果/输出表表表4 表1被截断并每天加载新数据 表1列col1、col2、col3与表2列col7、col18、col9和表3列col9、col11、col7相似,因此我们可以加载表2和表3中的匹配行 问题 循环table1以检查每一行col1、col2、col3是否与table2 col7、col18、col9中的任何行匹配,然后在table4中添加包含信息table1.col1、table1.col2、table1.col3、table2.col6、table3.col1、table3.col7的行,并添加一列以指示其更新 如果表1中的col1、col2、col3行在表2中不存在,则在表4中添加包含信息table1.col1、table1.col2、table1.col3、table2.col6、table3.col1、table3.col7的行,并添加一列以指示其添加 如果表2中的col7、col18、col9行在表1中不存在,则在表4中添加包含信息table1.col1、table1.col2、table1.col3、table2.col6、table3.col1、table3.col7的行,并添加一列以指示其删除 返回表4作为结果将表与sql Server 2012中的sql/tsql进行比较,sql,sql-server,tsql,Sql,Sql Server,Tsql,SQL Server 2012中有四个表:表1、表2、表3和结果/输出表表表4 表1被截断并每天加载新数据 表1列col1、col2、col3与表2列col7、col18、col9和表3列col9、col11、col7相似,因此我们可以加载表2和表3中的匹配行 问题 循环table1以检查每一行col1、col2、col3是否与table2 col7、col18、col9中的任何行匹配,然后在table4中添加包含信息table1.col1、table1.col2、table1.col3、tab
我们如何仅使用SQL/TSQL来实现这一点?本质上,您正在寻找from子句中具有条件逻辑的完全联接: 你想对表3做什么还不是很清楚。您可能希望左联接它,而不是完全联接,条件是在前面两个表中都签入,例如:
left join table t3
on t3.col9 = coalesce(t1.col1, t2.col7)
and t3.col11 = coalesce(t1.col2, t2.col8)
and t3.col7 = coalesce(t1.col3, t2.col9)
请显示一些示例数据、预期结果和您的尝试。我们很乐意为您提供帮助,但不是为您提供帮助。如果表3中有任何行与table1.col4=table3.col16和table1.col10=table3匹配,那么它只会提供结果的信息col7、col9和col11。col19@huby115:好的,我怀疑这一点,所以您希望在我的答案末尾使用左连接解决方案。
left join table t3
on t3.col9 = coalesce(t1.col1, t2.col7)
and t3.col11 = coalesce(t1.col2, t2.col8)
and t3.col7 = coalesce(t1.col3, t2.col9)