获取表1中的行不匹配表2在多列上使用JOIN(Getting the Rows in Table 1 That does not Match Table 2 Using JOIN on Multipl

编程入门 行业动态 更新时间:2024-10-26 01:32:09
获取表1中的行不匹配表2在多列上使用JOIN(Getting the Rows in Table 1 That does not Match Table 2 Using JOIN on Multiple Columns)

嗨我有这样的查询:

SELECT TT2.whs_code, TT2.pdt_code, TT2.fresh_frozen_status, TT2.case_dt_yyyymmdd, TT2.qty_cases, TT2.qty_wt FROM #TempTable2 AS TT2 LEFT OUTER JOIN #TempTable AS TT1 ON TT1.whs_code = TT2.whs_code AND TT1.pdt_code = TT2.pdt_code AND TT1.fresh_frozen_status = TT2.fresh_frozen_status AND TT1.case_dt_yyyymmdd = TT2.case_dt_yyyymmdd

事情是我想在#TempTable2中选择#TempTable1中不存在的列

whs_code, pdt_code, fresh_frozen_status, case_dt_yyyymmdd

让我们说一组数据1,115G,FR,20160222在#TempTable1中所以如果一组数据说 必须选择1,115G,FR,20160223或 必须选择1,115G,FZ,20160223或 必须选择2,115G,FR,20160223 但不是1,115G,FR,20160222

Hi I have the Query Like this :

SELECT TT2.whs_code, TT2.pdt_code, TT2.fresh_frozen_status, TT2.case_dt_yyyymmdd, TT2.qty_cases, TT2.qty_wt FROM #TempTable2 AS TT2 LEFT OUTER JOIN #TempTable AS TT1 ON TT1.whs_code = TT2.whs_code AND TT1.pdt_code = TT2.pdt_code AND TT1.fresh_frozen_status = TT2.fresh_frozen_status AND TT1.case_dt_yyyymmdd = TT2.case_dt_yyyymmdd

The Thing is I want to select the columns in #TempTable2 That are not existed in #TempTable1 based on

whs_code, pdt_code, fresh_frozen_status, case_dt_yyyymmdd

means that lets say a set of data 1,115G,FR,20160222 is in #TempTable1 So If a set of data Say 1,115G,FR,20160223 has to be selected or 1,115G,FZ,20160223 has to be selected or 2,115G,FR,20160223 has to be selected but not 1,115G,FR,20160222

最满意答案

我会使用NOT EXISTS。

SELECT TT2.whs_code, TT2.pdt_code, TT2.fresh_frozen_status, TT2.case_dt_yyyymmdd, TT2.qty_cases, TT2.qty_wt FROM #TempTable2 AS TT2 WHERE NOT EXISTS ( SELECT TOP 1 * FROM #TempTable AS TT1 WHERE tt1.whs_code = tt2.whs_code AND tt1.pdt_code = tt2.pdt_code AND TT1.fresh_frozen_status = tt2.fresh_frozen_status AND TT1.case_dt_yyyymmdd = TT2.case_dt_yyyymmdd )

I would use NOT EXISTS.

SELECT TT2.whs_code, TT2.pdt_code, TT2.fresh_frozen_status, TT2.case_dt_yyyymmdd, TT2.qty_cases, TT2.qty_wt FROM #TempTable2 AS TT2 WHERE NOT EXISTS ( SELECT TOP 1 * FROM #TempTable AS TT1 WHERE tt1.whs_code = tt2.whs_code AND tt1.pdt_code = tt2.pdt_code AND TT1.fresh_frozen_status = tt2.fresh_frozen_status AND TT1.case_dt_yyyymmdd = TT2.case_dt_yyyymmdd )

更多推荐

本文发布于:2023-08-06 18:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1453876.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不匹配   JOIN   多列上   Rows   Columns

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!