sql - Within the same group find and exclude records that have the same parent ID for certain types -
i have table following:
groupid parentid type 1 abc ind 1 abc ind 1 cde ord 1 efg std 2 zzz ind 2 zzz ind 2 zzz ind 3 yyy cor 3 yyy cor
i need exclude records in same group, having same parent id , type ind or cor. need keep groups have different parent id , type not ind or cor.
so result want following:
groupid parentid type 1 abc ind 1 abc ind 1 cde ord 1 efg std
somehow thinking use rank () over(partition groupid order parentid)
, won't give me results want.
any thoughts? ps: table has 5 million+ records. looking effective way deal it.
thanks
the following gives list of groupids want exclude
select groupid ( select groupid, count(distinct parentid) pcount, count(distinct typecode) tcount, max(typecode) tcode tablename group groupid ) t pcount = 1 , tcount = 1 , (tcode = 'ind' or tcode = 'cor')
now select else
select * tablename groupid not in ( select groupid ( select groupid, count(distinct parentid) pcount, count(distinct typecode) tcount, max(typecode) tcode tablename group groupid ) t pcount = 1 , tcount = 1 , (tcode = 'ind' or tcode = 'cor') )
test fiddle --> http://sqlfiddle.com/#!3/f1d4f/15/0
Comments
Post a Comment