如何在sql中的NOT EXISTS中使用where子句?

时间:2012-05-07 13:49:08

标签: sql sql-server

我需要将条件放在sql。{/ p>中的NOT EXISTS子句中

在下面需要检查以下sql查询中的重复记录 我需要把Date ='2012-05-07'和SecurityId ='52211' 但问题是使用内部连接而且我是新的bie没有得到如何把这些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'                 
    )

1 个答案:

答案 0 :(得分: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)

<击>