I have a table like below (using 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)
I need to select all the ID which are having all IsBad = 1. So, querying above table will return ID: 101 . It must not return 102 or 100 because these IDs are having at least one IsBad=0.
I tried below query
select id,count(distinct isBad) as Total
from Data
group by id
having count(distinct isBad)= 1
This query includes the IDs which are having all IsBad=0. but I dont need that. I tried to add some more conditions in having clause using AND , but getting error.
How to proceed ? Any help is appreciated.
EDIT: I need to run the query against a table having 50 Million records. So, the query needs to be optimized to return the result in less time.
Live example at SQL Fiddle.
If you’re just looking for the
id, you can use: