有条款的多个条件

时间:2013-01-29 09:18:37

标签: sql sql-server-2008-r2

我有一个如下表(使用SQL Server 2008 R2):

CREATE TABLE [dbo].[Data](
    [Id] [int] NOT NULL,
    [Name] [nvarchar](100) NOT NULL,
    [IsBad] [bit] NOT NULL
) ON [PRIMARY]
GO

Insert into Data  values(100,'Book!',1)
Insert into Data  values(100,'Booklki**',1)
Insert into Data  values(100,'Book',0)
Insert into Data  values(100,'New Book ~~',1)
Insert into Data  values(100,'New Book',0)
Insert into Data  values(100,'B00k…>',1)
Insert into Data  values(101,'Tim3#',1)
Insert into Data  values(101,'%Timer%',1)
Insert into Data  values(101,'T1mer**',1)
Insert into Data  values(101,'Tim6',1)
Insert into Data  values(101,'Time@me',1)
Insert into Data  values(102,'ABC',0)
Insert into Data  values(102,'CDE',0)

我需要选择所有ID的所有IsBad = 1。因此,查询上表将返回ID:101。它不得返回102100,因为这些ID至少有一个IsBad=0

我在下面尝试了查询

select id,count(distinct isBad) as Total
from Data
group by id
having count(distinct isBad)= 1 

此查询包含具有全部IsBad=0的ID。但我不需要那个。我尝试使用havingAND子句中添加更多条件,但收到错误。

如何进行?任何帮助表示赞赏。

编辑:我需要针对具有5000万条记录的表运行查询。因此,需要优化查询以在更短的时间内返回结果。

3 个答案:

答案 0 :(得分:2)

select  *
from    Data d1
where   not exists 
        (
        select  *
        from    Data d2
        where   d1.id = d2.id
                and d2.IsBad = 0
        )

Live example at SQL Fiddle.

如果您只是在寻找id,可以使用:

select  distinct id
... rest of the query is the same ...

答案 1 :(得分:2)

反转它 - 你想要“所有拥有所有IsBad = 1的ID”,这意味着ID不能有任何IsBad = 0:

SELECT ID FROM Data WHERE ID NOT IN (
    SELECT ID FROM Data WHERE IsBad = 0
)

答案 2 :(得分:0)

当前答案的缓慢可能是由于使用了where not exists条款。我通常使用left join并检查是否缺少匹配来解决此性能问题。

select *
from Data d1
left join (select * from Data where IsBad = 0) d2
  on d1.id = d2.id
where d2.id is null

这是一个老帖子,所以它可能无法帮助原始个体,但也许其他人会受益。

相关问题