Mysql Pivot在1个表中的一些列

时间:2018-04-02 15:22:11

标签: mysql sql pivot pivot-table

我有一张这样的表:

id       key       year   month   value
---------------------------------------
1        AD        2000    1       5465
2        AD        2000    2       6445
3        JK        2000    1       7777
4        JK        2000    2       9999

我需要检索这样的值:

key       2000-1  2000-2
------------------------
AD        5465    6445
JK        7777    9999

我在创建标题时遇到问题,连接yearmonth并在标题下显示value

我有另一个像这样的透视程序:

SELECT
    GROUP_CONCAT(
        DISTINCT CONCAT(
            'MAX(IF(combustible_id = ''',
            combustible_id,
            ''', valor_combustible, NULL)) AS ',
            CONCAT("`",admin_combustibles.nombre,"`")
        )
    ) INTO @SQL
FROM
    admin_indice_combustibles
INNER JOIN admin_combustibles 
   ON admin_indice_combustibles.combustible_id = admin_combustibles.id_combustible 
WHERE admin_indice_combustibles.estado = 1;

SET @SQL = CONCAT(
    'SELECT anio, mes, ',
    @SQL,
    ' FROM admin_indice_combustibles
      WHERE estado = 1 
      GROUP BY anio, mes 
      ORDER BY id_indice_combustible'
);

PREPARE stmt
FROM
    @SQL;

它正在工作,但它使用了更多数据(因为它与另一个表有一个JOIN),现在更容易,所有数据只在一个表中,但我无法得到它。有什么提示吗?

编辑:

我正在尝试使用此代码:

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),并为它们提供值(energia_adjudicada_mwh),每年和mont,按键分组...

这是我拥有的表格和我需要的表格: enter image description here

1 个答案:

答案 0 :(得分:1)

像这样使用条件聚合:

<强> SQL DEMO

Sub Test()
Dim NAME As String
Dim File_Path As String
Dim StrFile As String

    NAME = InputBox(" Enter Your NAME (EX: JOHNP) ")
    File_Path = "G:\NEWFOLDER\NAMEFOLDER\" & NAME & "\"

    StrFile = Dir(File_Path)

    If Dir(File_Path) <> "" Then
        Do While Len(StrFile) > 0
            If InStr(StrFile, ".doc") > 0 Or _
               InStr(StrFile, ".pdf") > 0 Then
                Debug.Print StrFile
                'ThisWorkbook.Sheets("Sheet2").Range("D5") = ("Checked")
                'ThisWorkbook.Sheets("Sheet2").Range("E5") = NAME

                If InStr(StrFile, ".doc") > 0 Then
                    Set wordapp = CreateObject("word.Application")
                    wordapp.documents.Open File_Path & StrFile
                    wordapp.Visible = True
                    Exit Do
                End If
            End If
            StrFile = Dir
        Loop
    Else
        MsgBox NAME & " Not found"
    End If
End Sub

<强>输出:

enter image description here