Search code examples
mysqlmultiple-columns

Consider values from multiple columns into 1 final output


Requirement:

(Related to: MySQL)

I have set of unique names in column A, then column B,.. with each column containing a unique product. Eg. enter image description here

I expect an output, that considers the total of all Columns, under 1 single header and outputs something like this: enter image description here

Is this possible? How do I go about that?

Thank you!

Not really sure how to go about this. Would concat/ Union work here?


Solution

  • select  person, sum(item) from 
    (SELECT person_1 as person, shoes as item from YOURTABLE 
    union SELECT person_2 as person, watches as item from YOURTABLE
    union SELECT person_3 as person, clothes as item from YOURTABLE) 
    temp group by person