This one has had me stumped for the last few hours and at this stage I think I need some help...
I need to compare multiple groups from a single table and to identify where items listed in col B match. For example: -
Col A...............Col B
John................Apple
John................Orange
John................Banana
Mary................Orange
Mary................Strawberry
David...............Apple
David...............Orange
David...............Banana
I want 'John' and 'David' returned because their items in col B match. Hope this makes sense! Thanks in advance! G
Compare Two Columns With a VLOOKUP Function and Find Matching Data. Another way to have Excel find duplicates in two columns is to use a VLOOKUP function. Excel will compare each cell in the second column against the cells in the first column. Use the =VLOOKUP(B2,$A$2:$A$14,1,0) for the column displaying the results.
Here's the SQL Fiddle for this solution, so that you can play with it yourself.
select A.ColA Person1, B.ColA Person2
from (select ColA, count(ColB) CountBs
from tbl
group by ColA) G1
join (select ColA, count(ColB) CountBs
from tbl
group by ColA) G2 on G1.ColA < G2.ColA
and G1.CountBs = G2.CountBs
join tbl A on A.ColA = G1.ColA
join tbl B on B.ColA = G2.ColA and A.ColB = B.ColB
group by A.ColA, B.ColA, G1.CountBs
having count(distinct A.ColB) = G1.CountBs
-- subqueries G1 and G2 are the same and count the expected colB's per colA
-- G1 and G2 are joined together to get the candidate matches
-- of ColA with the same number of ColB's
-- we then use G1 and G2 to join into tbl, and further join
-- between A and B where the ColB's match
-- finally, we count the matches between A and B and make sure the counts match
-- the expected count of B's for the pairing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With