如何添加几列并获得最大值?

问题描述 投票:-2回答:2

下面的图片显示了一个由6列数字组成的表格。

enter image description here

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;

此代码在一个小表中很有用,如下所示。

enter image description here

[不幸的是,由于代码行太长,所以我无法执行类似的代码来获得400列的最大值(而不是示例中的5列)。

所以,我想请您帮忙编写代码,以获取一列中的最大值,然后将此列添加到上一列中并获取最大值,依此类推...最多增加400列并获得最大值。

谢谢。

更新1(数据为文本)。主键: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

mysql max
2个回答
0
投票

像您一样,仅显示col_2到col_6的示例。完整查询将很长,但并非不可能。

select max(col_6), max(col_5_on), max(col_4_on), max(col_3_on), max(col_2_on)
from (
    select
        id,
        sum(if(col>5,val,0)) col_6,
        sum(if(col>4,val,0)) col_5_on,
        sum(if(col>3,val,0)) col_4_on,
        sum(if(col>2,val,0)) col_3_on,
        sum(if(col>1,val,0)) col_2_on
    from (
        select id, 2 col, col_2 val from table_1
        union all
        select id, 3 col, col_3 val from table_1
        union all
        select id, 4 col, col_4 val from table_1
        union all
        select id, 5 col, col_5 val from table_1
        union all
        select id, 6 col, col_6 val from table_1
    ) split_data
    group by id
) summed_data;

0
投票

很容易

通过此过程

DROP procedure IF EXISTS `proc_add_userdata`;

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_add_userdata`(IN Number_columns INTEGER)
BEGIN
    DECLARE icount INTEGER DEFAULT Number_columns - 1;
    DECLARE addtext  LONGTEXT DEFAULT '';
    SET @sql = CONCAT('SELECT MAX(col_',Number_columns,'),');
    SET addtext = CONCAT('col_',Number_columns, ' + ');
    loop_label:  LOOP
        IF icount = 1 THEN
            SET addtext = CONCAT(addtext,'col_',icount);
            SET @sql = CONCAT(@sql,'MAX(',addtext, ') FROM userdata;');
            LEAVE  loop_label;
        END IF;
        SET addtext = CONCAT(addtext,'col_',icount);
        SET @sql = CONCAT(@sql,'MAX(',addtext, ' ),');
        SET addtext = CONCAT(addtext,' + ');
        SET icount = icount - 1;
    END LOOP;
    PREPARE stmt1 FROM @sql;
    EXECUTE stmt1; 
END$$

DELIMITER ;

和此电话

CALL proc_add_userdata(10);

该过程将构建并运行此查询

SELECT 
    MAX(col_10)
    ,MAX(col_10 + col_9 )
    ,MAX(col_10 + col_9 + col_8 )
    ,MAX(col_10 + col_9 + col_8 + col_7 )
    ,MAX(col_10 + col_9 + col_8 + col_7 + col_6 )
    ,MAX(col_10 + col_9 + col_8 + col_7 + col_6 + col_5 )
    ,MAX(col_10 + col_9 + col_8 + col_7 + col_6 + col_5 + col_4 )
    ,MAX(col_10 + col_9 + col_8 + col_7 + col_6 + col_5 + col_4 + col_3 )
    ,MAX(col_10 + col_9 + col_8 + col_7 + col_6 + col_5 + col_4 + col_3 + col_2 )
    ,MAX(col_10 + col_9 + col_8 + col_7 + col_6 + col_5 + col_4 + col_3 + col_2 + col_1) 
FROM userdata;

结果

MAX(col_10) MAX(col_10 + col_9 )    MAX(col_10 + col_9 + col_8 )    MAX(col_10 + col_9 + col_8 + col_7 )    MAX(col_10 + col_9 + col_8 + col_7 + col_6 )    MAX(col_10 + col_9 + col_8 + col_7 + col_6 + col_5 )    MAX(col_10 + col_9 + col_8 + col_7 + col_6 + col_5 + col_4 )    MAX(col_10 + col_9 + col_8 + col_7 + col_6 + col_5 + col_4 + col_3 )    MAX(col_10 + col_9 + col_8 + col_7 + col_6 + col_5 + col_4 + col_3 + col_2 )    MAX(col_10 + col_9 + col_8 + col_7 + col_6 + col_5 + col_4 + col_3 + col_2 + col_1)
46          73                     91                               101                 133 170 200 241 262 283

您得到图片。

您可以改进存储过程,以获取要查询的列数,请参见https://stackoverflow.com/a/10492187/5193536

Alo,您可以为每个最大值购买别名(为icount = 1)和'c _',icount添加一个别名”>

出于调试目的,请注释“准备和执行” ny添加-并添加SELECT @sql行;这样您就可以获取文字并可以检查错误了。

© www.soinside.com 2019 - 2024. All rights reserved.