The below picture shows a table with 6 columns filled with numbers.
SELECT MAX(col_6) AS c1,
MAX(col_6 + col_5) AS c2,
MAX(col_6 + col_5 + col_4) AS c3,
MAX(col_6 + col_5 + col_4 + col_3) AS c4,
MAX(col_6 + col_5 + col_4 + col_3 + col_2) AS c5
INTO OUTFILE 'path/to/file.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM table_1;
This code is usefull in a small table, as shown below.
Unfortunately, I am not able to execute a similar code to get the maximum values in 400 columns (instead of 5 columns as shown in the example), as the code line would be too long.
So, I would like to ask your help to write a code in order to get the maximum value in a column, then add this column to the previous one and get the maximum value, and so on... up to add 400 columns and get the maximum value.
Thanks.
Update 1 (data as text). Primary key: id
id,col_1,col_2,col_3,col_4,col_5,col_6,col_7,col_8,col_9,col_10
1,10,6,19,34,49,3,20,7,2,46
2,3,21,41,39,35,25,14,36,40,11
3,44,3,15,19,21,31,8,18,30,43
4,24,17,6,46,28,18,13,8,45,5
5,39,42,22,10,37,38,20,19,23,33