SQL: Counting many columns and suming the result?

Soldato
Joined
16 Nov 2003
Posts
9,682
Location
On the pale blue dot
A possibly daft one for you. Consider the following table with fields a, b, c, d and e:

a b c d e
---------
a e t s p
e a v s o
a v t o s

Each of the five fields contains the same type of data as the other four. What I want to do is count the number of occurrences of a value in each column and then sum the result. Counting column a would be 'SELECT COUNT(a) FROM table GROUP BY a'. I think I could do this statement for all five columns and UNION them together but it looks messy. Is there an easier way I haven't thought of?
 
MyQL unfortunately, though a quick Google suggests MySQL 5 supports stored procedures, I'll have a look.

I do spend a lot of time thinking 'I could do this faster in SQL 2005' :D
 
Back
Top Bottom