Combining multiple column

First of all I apologize for my wrong English. I have a problem that I have been trying to solve for about a year. I tried to solve it with 2 different scenarios but I could not speed it up.

Scenario 1

I am trying to achieve successful results by combining the columns of 2 tables with approximately 1000 columns. With the help of c #, I group the columns in the table and query them in order. I sum the result column, count the id column, and find the ratio by dividing the result column by the id column. If the ratio is 80 and above and the result sum is greater than 3, I add it to the list. If not, and if the counted id is greater than 20 and the sum of result is greater than 3, I add another column to the grouping. So I continued querying with subqueries.

S1 – Sample Table

``id  result  col1  col2  col3 ... col1000 1    1       2     1     2        2 2    0       2     2     1        0 3    1       2     2     0        2 4    0       0     0     1        2 5    0       2     2     1        0 6    1       1     2     1        2 7    1       3     2     3        2 ``

S1 – Sample Query

``select col1,col2, ... ,  count(id) cnt, sum(result) sm,  (100*sum(result)/cnt) avgg  from sample  group by col1,col2, ... having sum(result)>2  ``

S1 – Sample Output

``╔══════╦══════╦═════╦═════╦════╦══════╗ ║ col1 ║ col2 ║ ... ║ cnt ║ sm ║ avgg ║ ╠══════╬══════╬═════╬═════╬════╬══════╣ ║  2   ║  2   ║ ... ║ 10  ║ 6  ║ 60   ║ ║  2   ║  1   ║ ... ║  8  ║ 7  ║ 88   ║ ║  1   ║  2   ║ ... ║  6  ║ 6  ║ 100  ║ ║  1   ║  1   ║ ... ║  9  ║ 3  ║ 33   ║ ║  3   ║  2   ║ ... ║  4  ║ 3  ║ 75   ║ ║  0   ║  1   ║ ... ║  7  ║ 4  ║ 57   ║ ╚══════╩══════╩═════╩═════╩════╩══════╝ ``

Scenario 2

Since the process took too long in scenario 1, I developed scenario 2. In this, I created a table with 4 columns and automatically assigned an id to each record. I have directly typed the ids of the previous tables into the column named ‘mid’ and the result column to the column that takes the name of each column. Again in this scenario, I developed it with c #.

S2 – Sample Table

``id  mid    colname  result  1    1       col3     0     2    2       col4     1      3    3       col1     0      4    4       col2     1      5    1       col4     0      6    3       col2     0      7    4       col1     1      ``

S2 – Sample Query

``select concat('parent_cols',',',colname), max(id) maxid, sum(result) summ  from sample  where mid in ('parent mids') and id>('parent maxid') GROUP BY colname having sum(result)>2  order by maxid  ``

In this scenario, I am trying to find successful results with subqueries with c #. this scenario came faster, but both scenarios take days for a single review.

Edit1: Scenario 1 350k rows, Scenario 1 to Scenario 2 converting 13m rows.

How can I speed up this process, I have been searching for a solution whenever I have the opportunity for about 1 year. Thank you in advance for putting up with my bad English and helping.