Let's say I have a table of (mostly) faithful couples. The couples appear several times in the same table.
You can tell who's faithful because Jack is always with Dianne, Fred is always with Ginger, etc.
However, Tom, Dick, and Harry are each dating Ms Homewrecker. Like this:
Fred Ginger
Fred Ginger
Fred Ginger
Jack Dianne
Jack Dianne
Tom Homewrecker
Dick Homewrecker
Harry Homewrecker
Bogie Bacall
Heathcliff Catherine
Heathcliff Catherine
Heathcliff Catherine
Heathcliff Catherine
Adam Steve
Adam Steve
I can start by GROUPing by column Y:
select X, Y from couples group by Y
which yields:
X Y
Fred Ginger
Jack Dianne
Tom Homewrecker
Dick Homewrecker
Harry Homewrecker
Bogie Bacall
Heathcliff Catherine
Adam Steve
However, I would like to get a third column which shows the total number of different partners each Y has.
That way, faithful couples will show as always having only one partner.
You can spot the homewrecker because they have > 1 partner.
X Y Count
Fred Ginger 1
Jack Dianne 1
Tom Homewrecker 3 <--- THERE'S the b***h!!!
Dick Homewrecker 3 <--- THERE'S the b***h!!!
Harry Homewrecker 3 <--- THERE'S the b***h!!!
Bogie Bacall 1
Heathcliff Catherine 1
Adam Steve 1
There must be something I really don't understand, because I've been going blind been trying every combination of SELECT, DISTINCT, GROUP BY, ROLLUP, COUNT and so on.