0

MySQL table is as follows

A | B1|B2|
-----------
0 | 1 |1 |
1 | 0 |1 |
1 | 0 |1 |
1 | 0 |0 |

I need to transpose my data in a table which contains only 2 columns: labels and sum of the original column contents such as

codes | sums
------------
A     | 3
B1    | 1
B2    | 3

I don't handle very much MySQL syntax and I don't find the proper query. I can get the sum of the column, but I still don't understand how to transpose the column's name

Elena Politi
  • 161
  • 2
  • 18

1 Answers1

2

This can be a possible solution:

SELECT 'A' as `codes`, SUM(`A`) as `sums` FROM `your_table_name`
UNION ALL
SELECT 'B1' as `codes`, SUM(`B1`) as `sums` FROM `your_table_name`
UNION ALL
SELECT 'B2' as `codes`, SUM(`B2`) as `sums` FROM `your_table_name`
Dan
  • 3,329
  • 2
  • 21
  • 28