SQL ORACLE将列转换为行

时间:2018-11-25 09:16:15

标签: sql oracle pivot unpivot

我在Indice,valeur1等列中得到了结果:

指标值1值2值3值4

1 x数学物理化学

18年2月1日133184115

我想以垂直方式显示结果

Indice,1,2
valeur1, x,21/01/2018
valeur2,maths, 133
valeur3,physics,184
valeur4,chemistry ,115
...
...
...
...

我的查询:

    select
    'x' as Valeur1,
    'maths' as Valeur2,
    'physics' as Valeur3, 
    'Chemistry' as Valeur4, 
    null as Valeur5, 
    null as Valeur6, 
    null as Valeur7, 
    null as Valeur8, 
    null as Valeur9, 
    null as Valeur10 
    from dual
    union
    select
        2 as indice,
      to_char((x) ) as Valeur1,
      to_char(sum(maths)) as valeur2,
      to_char(sum(physics)) as valeur3,
      to_char(sum(chemisty)) as valeur4,
      null as Valeur5, 
      null as Valeur6, 
      null as Valeur7, 
      null as Valeur8, 
      null as Valeur9, 
      null as Valeur10 

 from t1

我想将列转置为结果中的行。 您能否建议我可能的解决方案。

2 个答案:

答案 0 :(得分:2)

您需要UNPIVOT

select * from unpivotdemo;

    INDICE VALEUR1              VALEUR2              VALEUR3              VALEUR4              VALEUR5              VALEUR6              VALEUR7              VALEUR8              VALEUR9              VALEUR10
---------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------
         1 Semaine courante     Semaine precedente   Semaine -2                                                                                                                                         
         2 03/10/18             184                  115                                                                                                                                                

select indice, valeur, description
from   unpivotdemo
unpivot
     ( description for valeur in
       ( valeur1
       , valeur2
       , valeur3
       , valeur4
       , valeur5
       , valeur6
       , valeur7
       , valeur8
       , valeur9
       , valeur10 ) );

    INDICE VALEUR   DESCRIPTION
---------- -------- --------------------
         1 VALEUR1  Semaine courante
         1 VALEUR2  Semaine precedente
         1 VALEUR3  Semaine -2
         2 VALEUR1  03/10/18
         2 VALEUR2  184
         2 VALEUR3  115

6 rows selected

默认情况下,排除空值。您可以通过在include nulls关键字之后添加unpivot来包括它们。

要在INDICE列中包含VALEUR,可以将其从select列表移至unpivot ... in列表-但是您还需要转换数据类型以使其匹配:

select valeur, description
from   ( select to_char(indice) as indice
               , valeur1, valeur2, valeur3, valeur4, valeur5, valeur6, valeur7, valeur8, valeur9, valeur10
         from   unpivotdemo d )
unpivot include nulls
     ( description for valeur in
       ( indice
       , valeur1
       , valeur2
       , valeur3
       , valeur4
       , valeur5
       , valeur6
       , valeur7
       , valeur8
       , valeur9
       , valeur10 ) );

VALEUR   DESCRIPTION
-------- ----------------------------------------
INDICE   1
VALEUR1  Semaine courante
VALEUR2  Semaine precedente
VALEUR3  Semaine -2
VALEUR4  
VALEUR5  
VALEUR6  
VALEUR7  
VALEUR8  
VALEUR9  
VALEUR10 
INDICE   2
VALEUR1  03/10/18
VALEUR2  184
VALEUR3  115
VALEUR4  
VALEUR5  
VALEUR6  
VALEUR7  
VALEUR8  
VALEUR9  
VALEUR10 

22 rows selected

编辑以下扩展问题:

要将值聚合为逗号分隔的字符串,可以使用LISTAGG

select valeur, description
from   ( select listagg(indice, ',')   within group (order by indice) as indice
              , listagg(valeur1, ',')  within group (order by indice) as valeur1
              , listagg(valeur2, ',')  within group (order by indice) as valeur2
              , listagg(valeur3, ',')  within group (order by indice) as valeur3
              , listagg(valeur4, ',')  within group (order by indice) as valeur4
              , listagg(valeur5, ',')  within group (order by indice) as valeur5
              , listagg(valeur6, ',')  within group (order by indice) as valeur6
              , listagg(valeur7, ',')  within group (order by indice) as valeur7
              , listagg(valeur8, ',')  within group (order by indice) as valeur8
              , listagg(valeur9, ',')  within group (order by indice) as valeur9
              , listagg(valeur10, ',') within group (order by indice) as valeur10
         from   unpivotdemo )
unpivot include nulls
     ( description for valeur in
       ( indice
       , valeur1
       , valeur2
       , valeur3
       , valeur4
       , valeur5
       , valeur6
       , valeur7
       , valeur8
       , valeur9
       , valeur10 ) );

VALEUR   DESCRIPTION
-------- --------------------------------------------------------------------------------
INDICE   1,2
VALEUR1  Semaine courante,03/10/18
VALEUR2  Semaine precedente,184
VALEUR3  Semaine -2,115
VALEUR4  
VALEUR5  
VALEUR6  
VALEUR7  
VALEUR8  
VALEUR9  
VALEUR10 

11 rows selected

请注意,逗号分隔的字符串可能会变长,默认情况下,您只能使用4000个字符,根据示例数据,这可能是大约200行。

答案 1 :(得分:0)

您需要使用类似的

select * from (
    select indice from table union all
    select value1 from table union all
    select value2....union all
    select last from table
)

否则将unpivot用作

select column from table
unpivot ( column for column in (indice, value1, ... valuen) )