我已经检查过类似的情况,但不幸的是仍然没有:(,我尝试了一些代码,但仍然无法正常工作
表
TYPE |ATTR_NAME | ATTR_VALUE
**sample flat rate |Activity | N
**sample flat rate |code | PLAN_999
**sample flat rate |codes object | object
**sample low rate |Activity | Y
**sample low rate |code | PLAN_1299
**sample low rate |codes object | charge
**sample low rate |indicator | 0
代码:
declare
sqlqry VARCHAR2(4001);
cols VARCHAR2(4001);
begin
select listagg('''' || ATTR_NAME || ''' as "' || ATTR_NAME || '"', ',') within group (order by ATTR_NAME)
into cols
from (select distinct ATTR_NAME from temp_2);
sqlqry :=
'select * from
(
select *
from temp_2
)
pivot
(
MIN(ATTR_VALUE) for ATTR_NAME in (' || cols || ')
)'
;
答案 0 :(得分:0)
使用表格,如下所示:
create table temp_2 (TYPE, ATTR_NAME, ATTR_VALUE) as (
select 'sample flat rate', 'Activity' , 'N' from dual union all
select 'sample flat rate', 'code' , 'PLAN_999' from dual union all
select 'sample flat rate', 'codes object', 'object' from dual union all
select 'sample low rate', 'Activity' , 'Y' from dual union all
select 'sample low rate', 'code' , 'PLAN_1299' from dual union all
select 'sample low rate', 'codes object', 'charge' from dual union all
select 'sample low rate', 'indicator' , '0' from dual
)
这是一个有效的示例:
SQL> variable x refcursor
SQL>
SQL> declare
2 sqlqry VARCHAR2(4001);
3 cols VARCHAR2(4001);
4 begin
5 select listagg('''' || ATTR_NAME || ''' as "' || ATTR_NAME || '"', ',') within group (order by ATTR_NAME)
6 into cols
7 from (select distinct ATTR_NAME from temp_2);
8 sqlqry :=
9 'select * from
10 (
11 select *
12 from temp_2
13 )
14 pivot
15 (
16 MIN(ATTR_VALUE) for ATTR_NAME in (' || cols || ')
17 )';
18 open :x for sqlqry;
19 end;
20 /
PL/SQL procedure successfully completed.
SQL> print :x
TYPE Activity code codes obj indicator
---------------- --------- --------- --------- ---------
sample low rate Y PLAN_1299 charge 0
sample flat rate N PLAN_999 object
SQL>