Given the following table (how to format those correctly here?)
primary secondary
A a
A b
A b
B a
B a
B b
I'm trying to get comparitive group-by counts using a self join.
Getting the following result set is easy:
Primary Secondary Count
A a 1
A b 2
B a 2
B b 1
with something like:
select primary,secondary,count(*) from foobar group by primary,secondary
But what I REALLY want is this:
Primary Secondary Count Primary Secondary Count
A a 1 B a 2
A b 2 B b 1
When counts and group bys aren't involved, self-joins are simple. But I can't seem to navigate my way around doing this.
Does the "self join AFTER group by" make this impossible to do? If I have to play temp table games I'll do it (though I'd rather not) since the real goal is a single block of sql (something I can script), more than a single select statement.
At the moment I'm doing the former and manually padiddling the data.
Thoughts?
- M
Hmm... Of course all the stuff in my head is obvious to ME ;)
The "business logic" I'm trying to achieve is "compare the count of 'secondary' in 'primary A' to the count of 'secondary' in 'primary B' which is why I didn't write out the B:B result set lines. But I figure any clause that gets them in there can be filtered anyway.