Not in clause not filtering SQL Server with open query

Not in clause not filtering SQL Server with open query

所以我有两个 table 看起来像这样


库存table(这只是列出空位置)

|loc|

|ECA001|
_____

库存转移作业列表table

|id | oid | sku | from_loc | to_loc | tag | qty | processed |  create_date | processed date

|2  | 003 | 123 | SD034 |  |T100010| 2 | NULL | 2017-04-06 | NULL

___

我想要做的是填充 Inventory_Transfer_Job_List_Table。 To_loc 我的第一个 table 上的下一个空位置和我的 Inventory_Transfer_Job_List.to_loc 中尚未使用的位置 table

我现在正在处理 select 声明,以确保在更新之前我插入了正确的数据。

我试过了

select * from
    (select loc from openquery(wms7,'SELECT lc_f.loc FROM lc_f left join iv_f on 
    lc_f.loc = iv_f.loc WHERE lc_f.loc like ''EC%'' AND lc_f.loc not in   (select 
    loc from iv_f where loc like ''EC%'' OR loc = ''ECRETURNS'')')) as a , 
[db1].[dbo].[ECOM_Transfer_Inventory_Job_List]  as b
where to_loc = ''
and a.loc  not in (SELECT to_loc
               FROM   [db1].[dbo].[ECOM_Transfer_Inventory_Job_List] 
               WHERE  to_loc != '')

但我只得到一个重复值

ex.
loc | id | oid | sku | from_loc | to_loc | tag | qty | processed |  create_date | processed date

|ECA001| 1 | 0001 |0003 | MCA022 | | T100001| 2 | Null | 2017-04-06 | NULL

|ECA001| 7 | 0023 |0015 | MCA049 | | T100051| 12 | Null | 2017-04-06 | NULL

而不是仅仅过滤掉那些已经被使用过的。

帮助帮助会很好谢谢你,

这是您的查询的示例。

SELECT *
FROM (SELECT 1 a
        UNION ALL
      SELECT 2) a,
(SELECT 3 b
    UNION ALL
 SELECT 4) b 

尝试添加 WHERE Something in table a = something in table b

ON a.Column = b.Column

如果您有 SQL-Server 2008 + 或许像下面这样的东西会满足您的需要。

SELECT * FROM
(SELECT ROW_NUMBER() OVER(ORDER BY loc) AS RowNum, loc 
 FROM OPENQUERY(wms7,'SELECT lc_f.loc FROM lc_f LEFT JOIN iv_f ON 
lc_f.loc = iv_f.loc WHERE lc_f.loc LIKE ''EC%'' AND lc_f.loc NOT IN (SELECT 
loc FROM iv_f WHERE loc LIKE ''EC%'' OR loc = ''ECRETURNS'')')) AS a
JOIN (SELECT ROW_NUMBER() OVER(ORDER BY Id) AS RowNum, *
    FROM [db1].[dbo].[ECOM_Transfer_Inventory_Job_List])  AS b ON  a.RowNum = b.RowNum
WHERE b.to_loc = ''
  AND a.loc  NOT IN (SELECT to_loc
                     FROM   [db1].[dbo].[ECOM_Transfer_Inventory_Job_List] 
                     WHERE  to_loc != '')