1/11th of the readsCREATE TABLE #Sample ( custID INT, custName VARCHAR(4), mnth CHAR(6), ppFlag VARCHAR(5), amount INT )INSERT #SampleSELECT 1234, 'ABCD', 'Jan 08', 'True', 900 UNION ALLSELECT 1234, 'ABCD', 'MAR 08', 'True', 0 UNION ALLSELECT 1234, 'ABCD', 'Jun 08', 'True', 0 UNION ALLSELECT 1234, 'ABCD', 'Aug 08', 'True', 0 UNION ALLSELECT 2345, 'XYZ', 'Jan 08', 'True', 0 UNION ALLSELECT 2345, 'XYZ', 'FEB 08', 'True', 0 UNION ALLSELECT 2345, 'XYZ', 'SEP 08', 'True', 0 UNION ALLSELECT 6789, 'PQRS', 'Jan 08', 'True', 0 UNION ALLSELECT 6789, 'PQRS', 'Jun 08', 'True', 100 UNION ALLSELECT 6789, 'PQRS', 'OCT 08', 'FALSE', 1000 UNION ALLSELECT 6789, 'PQRS', 'NOV 08', 'False', 500create index ix_sample on #sample (custid, ppflag, amount)-- Habinglselect custid from #Sample where amount = 0 and ppflag = 'true'and custid not in (select custid from #Sample where amount <> 0 or ppflag = 'false')-- Madhiselect custid from #Sample as t1 where amount = 0 and ppflag = 'true'and not exists(select * from #Sample where (amount <> 0 or ppflag = 'false') and custid=t1.custid)-- PesoSELECT custIDFROM #SampleGROUP BY custIDHAVING MIN(CASE WHEN amount = 0 AND ppFlag = 'true' THEN 1 ELSE 0 END) = 1drop table #sample
E 12°55'05.63"N 56°04'39.26"