Keep in mind that there's no way I can normalize the table, so I'm having to work with what I got. In the table, the rows are similar to this
name | widget1 | widget2 | widget3
------+-----------+----------+----------
Joe | blue | red |
Jane | green | |
Bob | red | red | green
Susy | green | green |
What I'd like to do is count the total number of widgets (Joe has 2 widget, Jane has 1, etc), and also count the number of similar widgets (Bob has 3 widgets - 2 red and 1 green, Susy has 2 widgets- 2 green, etc)
Here's my code to count the total number of widgets:
SELECT (
SUM( IF(widget1 <> "", 1, 0) ) +
SUM( IF(widget2 <> "", 1, 0) ) +
SUM( IF(widget3 <> "", 1, 0) )
) AS totalWidgets FROM table
Which works fine. But is there a better way to do this? Also, to count the number of similar of values, I'm sure I can so something similar but just check if the values are equal... but it could get pretty long and convoluted.
Is there a more direct approach?