0

我需要将 where 条件放在NOT EXISTSsql 中的子句中。

在下面需要检查下面的 sql 查询中的重复记录,我需要输入 Date='2012-05-07' 和 SecurityId='52211' 但问题是使用了内部连接,我是新手,不知道如何把这些where子句请帮忙。

SELECT DISTINCT

    SecurityPriceId

FROM 
    dbo.Indicative Bond
    INNER JOIN 
    dbo.BondPrice BondPrice ON 
        Indicative.SecurityId = BondPrice.SecurityId AND
        BondPrice.SecurityPriceSourceId = @SecurityPriceSourceComposite
WHERE
    Date = @Date -- Date='2012-05-07' like this
    AND
    NOT EXISTS
    (
        SELECT
            'z'
        FROM
            dbo.Reporting_BondPrices
    WHERE
        Reporting_BondPrices.SecurityId = BondPrice.SecurityId AND
        Reporting_BondPrices.Date = BondPrice.Date 
        --how can i put Date='2012-05-07' and SecurityId='52211'                 
    )
4

1 回答 1

2

在您更新之后,我认为(??)您想要这个?

SELECT DISTINCT

    BondPrice.SecurityPriceId

FROM 
    dbo.Reporting_BondIndicative Reporting_BondIndicative
    INNER JOIN 
    dbo.BondPrice BondPrice ON 
        Reporting_BondIndicative.SecurityId = BondPrice.SecurityId AND
        BondPrice.SecurityPriceSourceId = @SecurityPriceSourceComposite
WHERE
    BondPrice.Date = @Date -- BondPrice.Date='2012-05-07' like this
    AND
    NOT EXISTS
    (
        SELECT
            'z'
        FROM
            dbo.Reporting_BondPrices
    WHERE
        Reporting_BondPrices.SecurityId = BondPrice.SecurityId AND
        Reporting_BondPrices.Date = BondPrice.Date 
        --how can i put Date='2012-05-07' and SecurityId='52211'                 
        --simply put them in with and
        and Reporting_BondPrices.SecurityId='52211' and Reporting_BondPrices.Date='20120507'
    )

早期尝试解码您的问题:

您可以给表起别名,如下所示:

select ...
from table as t1  --t1 will be the outer table
where not exists(select ...
                 from table as t1  --t2 will be the inner table
                 where t1.column1=t2.column1 and t1.column2<>t2.column2)

于 2012-05-07T13:54:09.257 回答