仅当 table B 中不存在特定值时,才从 table A 获取值

get value from table A only if a specific value not present in table B

我在 Table A 中有数据,但只有当 table B 不包含为 ID 主键提取的值时,我才需要获取此数据。

我已经尝试在 Table A 和 Table B 上进行 Left Join,并尝试使用 Table B 状态不具有撤回条件来过滤结果。

SELECT * from
from TableA
Left join TableB ON TableB.dealid  = TableA.DealID
where TableA.Status in ('Files Returned', 'Files Sent')
AND TableA.Type in ('Refinance', 'Purchase')
AND TableB.Status <> 'Withdrawn'

我仍然收到来自 table A 的一行,我不应该这样做。

Table A

FileID      Type        Amount      Status
4           Refinance   100190.00   Files Returned


Table B

Status                      FileID
Files Returned                4
Files Sent                    4
Instructions Received         4
Withdrawn                     4

我编辑答案。希望这能满足您的需求。

;
    WITH    cte
              AS ( SELECT   [a].[fileid] ,
                            [a].[type] ,
                            [a].[amount] ,
                            [a].[status] ,
                            [b].[status] AS 'statusB'
                   FROM     TableA AS a
                   INNER JOIN TableB AS b ON b.[FileId] = a.[fileid]
                   WHERE    a.status IN ( 'Files Returned', 'Files Sent' )
                            AND a.type IN ( 'Refinance', 'Purchase' )
                 )
        SELECT  [c].[fileid] ,
                [c].[type] ,
                [c].[amount] ,
                [c].[status]
        FROM    [cte] AS c
        WHERE   [c].[fileid] NOT IN ( SELECT DISTINCT
                                                [fileid]
                                      FROM      [cte]
                                      WHERE     [statusB] = 'Withdrawn' );