Having a hard time wording my issue. But finding duplicates within duplicates is about as close as i can get without an example.
ID,stringA,stringB,dID,oID 1,hello,apple,1,1 2,bye,apple,2,1 3,world,banana,1,1
- ID is the id of the row.
- stringA,stringB are just arbitrary strings.
- dID,oID are FK relationship to other tables.
I am trying to create a query that will give me all the rows where oID
is 1
and dID
is not unique and where stringA
OR stringB
is not unique. When finding not unique values for stringA
and stringB
the rows need to have matching dID
for that comparison. If the dID
does not match, we don’t care about those duplicates.
My expected query against the above dataset above would return nothing. Because there is no row where dID
is not unique but also has not unique values for stringA
or stringB
.
ID,stringA,stringB,dID,oID 1,hello,apple,1,1 2,bye,apple,2,1 3,world,banana,1,1 4,hello,null,1,1
My expected query against this given dataset would return both row 1 and 4. Because their oID
and dID
match and stringA
matches.
I have tried using nested selects using having count(*) > 1
on the fields but it keeps returning row 1 from the first dataset.
select * from table1 where oID = 1 and dID in (select dID from table1 where oID = 1 and dID > 0 group by dID having count(*) > 1) and ( stringA in (select stringA from table1 where oID = 1 and dID > 0 group by stringA having count(*) > 1) or stringB in (select stringB from table1 where oID = 1 and dID > 0 group by stringA having count(*) > 1) );
Any help is appreciated, thanks!
Corrected the result set.
Added more clarity to condition.
I thought this really ugly query was working, but turns out it is not working fully.
select * from t where stringB in (select stringB from (select * from (select * from t where (stringB in (select stringB from t where oID = 1 and dID > 0 group by stringB having count(*) > 1) or stringA in (select stringA from t where oID = 1 and dID > 0 group by stringA having count(*) > 1)) and dID in (select dID from t where oID = 1 and dID > 0 group by dID having count(*) > 1)) as foo) as bar where oID = 1 and dID > 0 group by stringB having count(*) > 1) or stringA in (select stringA from (select * from (select * from t where (stringB in (select stringB from t where oID = 1 and dID > 0 group by stringB having count(*) > 1) or stringA in (select stringA from t where oID = 1 and dID > 0 group by stringA having count(*) > 1)) and dID in (select dID from t where oID = 1 and dID > 0 group by dID having count(*) > 1)) as foo1) as bar1 where oID = 1 and dID > 0 group by stringA having count(*) > 1);
Advertisement
Answer
I found a solution that worked for now.
SELECT * FROM t WHERE( ((stringA in(select stringA from t WHERE dID is NOT NULL and oID = 5 group by stringA, dID having count(id) > 1)) OR (stringB in(select stringB from t WHERE dID IS NOT NULL and oID = 5 group by stringB, dID having count(id) > 1))) AND (dID in(select dID from t WHERE dID is NOT NULL and oID = 5 group by dID having count(id) > 1)) )