以 Oracle 10g 为中心

2022-01-22 00:00:00 pivot sql oracle oracle10g

我正在使用 oracle 10g.我有一个临时表 TEMP.

I am using oracle 10g. I have a temp table TEMP.

TEMP 的结构如下:-

TEMP has following structure:-

USER COUNT TYPE
---- ----- ----
   1    10   T1
   2    21   T2
   3    45   T1
   1     7   T1
   2     1   T3

我需要一个查询来显示所有类型都有列名,类型可以有任何值,如 T1, T2,..Tn 并且结果将是:-

I need a query which will show all types has column names,and types can have any value like T1, T2,..Tn and result will be like:-

USER T1 T2 T3
---- -- -- --
   1 17  0  0
   2  0 21  1
   3 45  0  0

和 User 列将显示所有用户,T1, T2 列将显示类型的总数.

and User column will show all the users and T1, T2 column will show total count of types.

推荐答案

在 Oracle 10g 中,没有 PIVOT 函数,但您可以使用带有 CASE:

In Oracle 10g, there was no PIVOT function but you can replicate it using an aggregate with a CASE:

select usr,
  sum(case when tp ='T1' then cnt else 0 end) T1,
  sum(case when tp ='T2' then cnt else 0 end) T2,
  sum(case when tp ='T3' then cnt else 0 end) T3
from temp
group by usr;

参见 SQL Fiddle with Demo

如果你有 Oracle 11g+ 那么你可以使用 PIVOT 函数:

If you have Oracle 11g+ then you can use the PIVOT function:

select *
from temp
pivot
(
  sum(cnt)
  for tp in ('T1', 'T2', 'T3')
) piv

参见 SQL Fiddle with Demo

如果您有未知数量的值要转换,那么您可以创建一个过程来生成它的动态版本:

If you have an unknown number of values to transform, then you can create a procedure to generate a dynamic version of this:

CREATE OR REPLACE procedure dynamic_pivot(p_cursor in out sys_refcursor)
as
    sql_query varchar2(1000) := 'select usr ';

    begin
        for x in (select distinct tp from temp order by 1)
        loop
            sql_query := sql_query ||
              ' , sum(case when tp = '''||x.tp||''' then cnt else 0 end) as '||x.tp;

                dbms_output.put_line(sql_query);
        end loop;

        sql_query := sql_query || ' from temp group by usr';

        open p_cursor for sql_query;
    end;
/

然后执行代码:

variable x refcursor
exec dynamic_pivot(:x)
print x

所有版本的结果都是一样的:

The result for all versions is the same:

| USR | T1 | T2 | T3 |
----------------------
|   1 | 17 |  0 |  0 |
|   2 |  0 | 21 |  1 |
|   3 | 45 |  0 |  0 |

根据您的评论,如果您想要 Total 字段,最简单的方法是将查询放在另一个 SELECT 中,类似于:

Based on your comment if you want a Total field, the easiest way is to place the query inside of another SELECT similar to this:

select usr,
  T1 + T2 + T3 as Total,
  T1,
  T2,
  T3
from
(
  select usr,
    sum(case when tp ='T1' then cnt else 0 end) T1,
    sum(case when tp ='T2' then cnt else 0 end) T2,
    sum(case when tp ='T3' then cnt else 0 end) T3
  from temp
  group by usr
) src;

参见 SQL Fiddle with Demo

相关文章