I have a table that has the following data (shortened for this example):
C1 C2 C3 C4
=========================
1 0 1 1 0
2 1 1 0 1
3 1 0 1 1
4 1 1 1 1
5 0 0 1 1
6 0 0 0 1
I want to create a create a query that gives me the following result:
C3 C4
=============
C1 2 3
C2 2 2
That is, the combination of those four columns when:
C1 = 1 & C3 = 1
C1 = 1 & C4 = 1
C2 = 1 & C3 = 1
C2 = 1 & C4 = 1
I’ve been able to get make some progress, but not ultimately what I want. I’ve managed to get this far: http://sqlfiddle.com/#!3/7d3d4/2/0
but I can’t figure out how to get my query to format like my desired output above. I was going to try to use a PIVOT table, but quickly abandoned that idea because it seemed to get extremely convoluted once I needed to add a 2nd PIVOT statement (and I’d end up having to add more as my example above is a really basic example of the number of columns I need to perform this on).
What am I missing?
Thank you.
try this:
SQL Fiddle Demo