Search code examples
mysqlloopsstring-concatenation

MYSQL: How could I iterate over table columns using a concatenated string as column name?


I am looking for a way to loop over columns in a MYSQL table using a concatenated string to index the column names within a loop. An example of the data in the table might look like this:

R1       R2     R3       R4     
0.68    0.08    0.04    4.24
0.15    1.17    3.06    0.57
1.59    0.48    1.39    1.00
1.59    0.77    1.13    0.22
0.90    0.23    0.73    0.52

The column names are simply the character "R" and a number (ultimately the number of columns will vary so the number could be up to 100). I would like to iterate over the columns to count the number of values in each column that exceed a threshold (I am using three thresholds: 3.0, 5.0 and 10.0). The result of calling the procedure should be something like the following:

str2    total   ThreeCount  FiveCount   TenCount
`R1`    8772    794            153        1

str2    total   ThreeCount  FiveCount   TenCount
`R2`    8772    382             42        0

etc.

I have been using the following code as a stored procedure to test the process (I am using 5 for the test and hence iterating over the first five columns of data values):

BEGIN
 DECLARE x  INT;
 DECLARE str1  VARCHAR(1);
 DECLARE str2  VARCHAR(5);
 DECLARE str3  VARCHAR(1);

 SET x = 1;
 SET str1 =  "R";
 SET str2 = "";
 SET str3 = "`";

 WHILE x  <= 5 DO
 SET  str2 = CONCAT(str3,str1,x,str3);
select str2,
    count(*) total,
    sum(case when str2 > 3.0 then 1 else 0 end) ThreeCount,
    sum(case when str2 > 5.0 then 1 else 0 end) FiveCount,
    sum(case when str2 > 10.0 then 1 else 0 end) TenCount
from test_dat;
SET  x = x + 1;
 END WHILE;

 END

However the procedure returns zero values (except for the total) unless I replace "str2" with the actual column names (e.g. replace str2 with R1, R2 etc). However I would rather not use the actual column names.

What could I do to to have my concatenated string (str2) recognised as a column name? I have tried various combinations of concatenation such as:

SET  str2 = CONCAT(str1,x);

But have not got non-zero counts with any of these.

Thank you for any help you are able to offer.


Solution

  • A normalised design might look something like this

    id r val 
     1 1 0.68    
     2 1 0.15    
     3 1 1.59    
     4 1 1.59    
     5 1 0.90    
    
     6 2 0.08    
     7 2 1.17    
     8 2 0.48    
     9 2 0.77    
    10 2 0.23 
    
    11 3 0.04    
    12 3 3.06    
    13 3 1.39    
    14 3 1.13    
    15 3 0.73    
    
    16 4 4.24
    17 4 0.57
    18 4 1.00
    19 4 0.22
    20 4 0.52