1

I have a table that has 5 different columns:

vote_1, vote_2, vote_3, vote_4 and vote_5

For each row, these columns contain a name and what I am trying to do now is fetch the total number of occurrences of every value in any of the above columns.

The final result should be something like:

Value1 | 30

Value2 | 25

.....

How to achieve this in a single SQL query?.

1
  • 2
    Any time you have enumerated columns (above '2', say) it's probably time to start rethinking your design. (Plus, we have different definitions of 'different') Commented Aug 26, 2016 at 15:06

1 Answer 1

3

Use UNION to combine multiple columns into one. Then GROUP and COUNT.

select vote, count(*) as cnt
from (
    select vote_1 as vote from mytable
    union all select vote_2 as vote from mytable
    ..
    union all select vote_N as vote from mytable
) sub
group by vote
Sign up to request clarification or add additional context in comments.

1 Comment

@ArtemisChaitidis You can thank him by selecting this as the correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.