Oracle SQL数据透视查询

时间:2011-01-30 07:29:42

标签: sql oracle pivot

我在表格中有数据如下所示:

MONTH VALUE

1     100
2     200
3     300
4     400
5     500
6     600

我想写一个SQL查询,结果如下:

MONTH_JAN MONTH_FEB MONTH_MAR MONTH_APR MONTH_MAY MONTH_JUN
100       200       300       400       500       600

3 个答案:

答案 0 :(得分:18)

Oracle 11g及以上

从Oracle 11g开始,您现在可以使用PIVOT运算符来实现该结果:

create table tq84_pivot (
  month number,
  value number
);

insert into tq84_pivot values(1, 100);
insert into tq84_pivot values(2, 200);
insert into tq84_pivot values(3, 300);
insert into tq84_pivot values(4, 400);
insert into tq84_pivot values(5, 500);
insert into tq84_pivot values(6, 600);
--
insert into tq84_pivot values(1, 400);
insert into tq84_pivot values(2, 350);
insert into tq84_pivot values(4, 150);

select 
  *
from
  tq84_pivot
pivot (
   sum (value) as sum_value for
     (month) in (1 as month_jan,
                 2 as month_feb,
                 3 as month_mar,
                 4 as month_apr,
                 5 as month_mai,
                 6 as month_jun,
                 7 as month_jul,
                 8 as month_aug,
                 9 as month_sep,
                10 as month_oct,
                11 as month_nov,
                12 as month_dec)
);

答案 1 :(得分:15)

Oracle 9i +支持:

SELECT SUM(CASE WHEN t.month = 1 THEN t.value ELSE 0 END) AS JAN,
       SUM(CASE WHEN t.month = 2 THEN t.value ELSE 0 END) AS FEB,
       SUM(CASE WHEN t.month = 3 THEN t.value ELSE 0 END) AS MAR,
       SUM(CASE WHEN t.month = 4 THEN t.value ELSE 0 END) AS APR,
       SUM(CASE WHEN t.month = 5 THEN t.value ELSE 0 END) AS MAY,
       SUM(CASE WHEN t.month = 6 THEN t.value ELSE 0 END) AS JUN
  FROM YOUR_TABLE t

你只列出两列 - 这样的东西应该按年分组。

有ANSI PIVOT(和UNPIVOT)语法,但Oracle直到11g才支持它。在9i之前,您必须使用Oracle特定的DECODE替换CASE语句。

答案 2 :(得分:1)

Dynamic Pivot for Oracle 11g+

在 Oracle 的 SQL 中没有直接的动态透视方法,除非它返回 XML 类型的结果。

对于non-XML结果PL/SQL可以通过创建SYS_REFCURSOR返回类型的函数来使用

  • 带有PIVOT子句

    CREATE OR REPLACE FUNCTION Get_Month_Values RETURN SYS_REFCURSOR IS
       v_recordset SYS_REFCURSOR;
       v_sql       VARCHAR2(32767);
       v_cols      VARCHAR2(32767);
    BEGIN
       SELECT LISTAGG( ''''||month||''' AS "MONTH_'||TO_CHAR( TO_DATE(month,'mm') ,'MON')||'"' , ',' )
                      WITHIN GROUP ( ORDER BY month )
         INTO v_cols
         FROM tab;
    
       v_sql :='SELECT *
                  FROM tab t
                 PIVOT
                 (
                  MAX(value) FOR month IN ( '|| v_cols ||' )
                 )';
    
       OPEN v_recordset FOR v_sql;
       DBMS_OUTPUT.PUT_LINE(v_sql);
       RETURN v_recordset;
    END;
    /
    
  • 使用条件聚合

    CREATE OR REPLACE FUNCTION Get_Month_Values RETURN SYS_REFCURSOR IS
       v_recordset SYS_REFCURSOR;
       v_sql       VARCHAR2(32767);
       v_cols      VARCHAR2(32767);
    BEGIN
       SELECT LISTAGG('MAX( CASE WHEN month = '''||month||''' THEN '||value||' END ) AS "MONTH_'||TO_CHAR( TO_DATE(month,'mm') ,'MON')||'"' , ',' )
                      WITHIN GROUP ( ORDER BY month )                 
         INTO v_cols
         FROM tab;
    
       v_sql :='SELECT '|| v_cols ||' FROM tab';
    
       OPEN v_recordset FOR v_sql;
       DBMS_OUTPUT.PUT_LINE(v_sql);
       RETURN v_recordset;
    END;
    /
    

然后该函数可以被调用为

VAR rc REFCURSOR
EXEC :rc := Get_Month_Values;
PRINT rc

来自SQL Developer的命令行

Demo