I have a mysql table as:
A | B | C | D | E | F |
---|---|---|---|---|---|
12 | 23 | 15 | 17 | 9 | 21 |
9 | 13 | 15 | 19 | 4 | 2 |
26 | 11 | 6 | 3 | 15 | 21 |
I want to add a new column, where the value is concatenation of the 6 column names, in descending order of the values, like this:
code |
---|
BFDCAE |
DCBAEF |
AFEBCD |
I tried using GREATEST() and LEAST() but that leads to at least 6!=720 lines of code, which I want to avoid. Is there a simple way to achieve this?
2
Answers
Here is how to do it using
UNION ALL
andGROUP_CONCAT
:Results :
Demo here
fiddle
If the table is more wide than provided one, and primary key exists in it, then it must be used instead of
rn
, andcte1
must be removed.Additional
col ASC
in ORDER BY makes the output deterministic.