I am having hard time writing the query that counts occurrence of specific value in multiple columns. I cannot change the structure of db. I know I can do this using multiple queries and union at the end, but I was wondering is there any smarter solution.
Here is an example.
+-----------+------------+------------+-------------+
| A | B |
C |
D |
+-----------+------------+------------+------------+
| value1 | value2 | value1 | value3 |
| value2 | value3 | value1 | value4 |
| value1 | value3 | value2 | value1 |
| value3 | value1 | value1 | value1 |
| value1 | value1 | value1 | value1 |
+-----------+------------+------------+------------+
I would like to count number of fields where value=value1 for each column. A result set should look like this:
+-----------+------------+
| Column | Count |
+-----------+------------+
| A | 3 |
| B | 2 |
| C | 4 |
| D | 3 |
+-----------+------------+
Thanks