Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by column A but comparing column B

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

like image 968
user1704276 Avatar asked Sep 27 '12 19:09

user1704276


People also ask

Is there a way to compare two columns in Excel for differences?

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.


1 Answers

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
like image 79
RichardTheKiwi Avatar answered Oct 27 '22 17:10

RichardTheKiwi