MysQL中的数据透视表,将两行名称连接成标题

时间:2018-04-02 19:52:30

标签: mysql sql mariadb pivot-table

我有这张桌子: 表:admin_contratos_energia_adjudicada_distribucion_mensual enter image description here

我需要一个程序来制作这个: enter image description here

我已经做了类似的事情,但现在我无法确定应该在哪里更改我的代码。我的实际代码就是这个:

BEGIN

SELECT
    GROUP_CONCAT(
        DISTINCT CONCAT(
            ' MAX(IF(anio = ''',
            DIST.anio,
            ''' AND mes = ''', DIST.mes, ''', energia_adjudicada_mwh, NULL)) AS ',
            CONCAT("`",DIST.anio,"-`", DIST.mes,"`")
        )
    ) INTO @SQL
FROM
    admin_contratos_energia_adjudicadas_distribucion_mensual AS DIST
WHERE DIST.activo = 1;

SET @SQL = CONCAT(
    'SELECT DIST.key, DIST.contrato_id ',
    @SQL,
    ' FROM admin_contratos_energia_adjudicadas_distribucion_mensual AS DIST 
        WHERE activo = 1 
        GROUP BY DIST.key 
        ORDER BY DIST.contrato_id ');

PREPARE stmt
FROM
    @SQL;

EXECUTE stmt;
DEALLOCATE PREPARE stmt;

END

我收到此错误:

程序执行失败 1064 - 您的SQL语法出错;检查与您的MariaDB服务器版本对应的手册,以便在MAX附近使用正确的语法(IF(anio =' 2016' AND mes =' 1',energia_adjudicada_mwh,NULL ))AS 2016- 1`,'在第1行

任何提示?我只需要连接' anio'和' mes'在新的标题中,放在那里的“能量调整”中。值。

请不要使用CASE发布答案,因为该表有20年的时间,我需要通过查询检索年份。

编辑:编辑了mes

附近的部分内容
BEGIN

SELECT
    GROUP_CONCAT(
        DISTINCT CONCAT(
            'MAX(IF(anio = ''',
            anio,
            ''' AND mes = ''', mes, ''', energia_adjudicada_mwh, NULL)) AS ',
            CONCAT("`",mes,"-", anio,"`")
        )
    ) INTO @SQL
FROM
    admin_contratos_energia_adjudicadas_distribucion_mensual
WHERE activo = 1;

SET @SQL = CONCAT(
    'SELECT key, contrato_id, ',
    @SQL,
    ' FROM admin_contratos_energia_adjudicadas_distribucion_mensual  
        WHERE activo = 1 
        GROUP BY key 
        ORDER BY contrato_id ');

PREPARE stmt
FROM
    @SQL;

EXECUTE stmt;
DEALLOCATE PREPARE stmt;

END

错误:

程序执行失败 1064 - 您的SQL语法出错;检查与您的MariaDB服务器版本相对应的手册,以便在' key,contrato_id,MAX附近使用正确的语法(IF(anio =' 2016' AND mes =' 1' ,energia_adjudicada_mwh,NU'在第1行

编辑2:

我选择了@SQL:

SELECT   key, 
         contrato_id, 
         Max(IF(anio = '2016' 
AND      mes = '1', energia_adjudicada_mwh, NULL)) AS `1-2016`, 
         Max(IF(anio = '2016' 
AND      mes = '2', energia_adjudicada_mwh, NULL)) AS `2-2016`, 
         Max(IF(anio = '2016' 
AND      mes = '3', energia_adjudicada_mwh, NULL)) AS `3-2016`, 
         Max(IF(anio = '2016' 
AND      mes = '4', energia_adjudicada_mwh, NULL)) AS `4-2016`, 
         Max(IF(anio = '2016' 
AND      mes = '5', energia_adjudicada_mwh, NULL)) AS `5-2016`, 
         Max(IF(anio = '2016' 
AND      mes = '6', energia_adjudicada_mwh, NULL)) AS `6-2016`, 
         Max(IF(anio = '2016' 
AND      mes = '7', energia_adjudicada_mwh, NULL)) AS `7-2016`, 
         Max(IF(anio = '2016' 
AND      mes = '8', energia_adjudicada_mwh, NULL)) AS `8-2016`, 
         Max(IF(anio = '2016' 
AND      mes = '9', energia_adjudicada_mwh, NULL)) AS `9-2016`, 
         Max(IF(anio = '2016' 
AND      mes = '10', energia_adjudicada_mwh, NULL)) AS `10-2016`, 
         Max(IF(anio = '2016' 
AND      mes = '11', energia_adjudicada_mwh, NULL)) AS `11-2016`, 
         Max(IF(anio = '2016' 
AND      mes = '12', energia_adjudicada_mwh, NULL)) AS `12-2016`, 
         Max(IF(anio = '2017' 
AND      mes = '1', energia_adjudicada_mwh, NULL)) AS 
from     admin_contratos_energia_adjudicadas_distribucion_mensual 
WHERE    activo = 1 
GROUP BY KEY 
ORDER BY contrato_id

最后编辑(正确答案):如果有人需要最终代码......就是这样:

BEGIN
SET @@group_concat_max_len = 9999;
SET @SQL = NULL;
SELECT
    GROUP_CONCAT(
        DISTINCT CONCAT(
            'MAX(IF(anio = ''',
            anio,
            ''' AND mes = ''', mes, ''', energia_adjudicada_mwh, NULL)) AS ',
            CONCAT("`",mes,"-", anio,"`")
        )
    ) INTO @SQL
FROM
    admin_contratos_energia_adjudicadas_distribucion_mensual
WHERE activo = 1;

SET @SQL = CONCAT(
    'SELECT `key`, contrato_id, ',
    @SQL,
    ' FROM admin_contratos_energia_adjudicadas_distribucion_mensual  
        WHERE activo = 1 
        GROUP BY `key` 
        ORDER BY contrato_id ');

PREPARE stmt
FROM
@SQL;

EXECUTE stmt;
DEALLOCATE PREPARE stmt;

#select @SQL;

END

0 个答案:

没有答案