日期:2014-05-16 浏览次数:20774 次
select Cust,
MAX(decode(prod,'A','A')) Prod1,
MAX(decode(prod,'B','B')) Prod2,
MAX(decode(prod,'C','C')) Prod3
from ax
GROUP BY Cust;
CUST PROD1 PROD2 PROD3
---------- ----- ----- -----
1 A B C
2 A B
--创建测试表 插入测试数据
create table tb1 (cust number,prod nvarchar2(5));
insert into tb1 values (1,'A');
insert into tb1 values (1,'B');
insert into tb1 values (1,'C');
insert into tb1 values (2,'A');
insert into tb1 values (2,'B');
commit;
--创建存储过程
CREATE OR REPLACE PROCEDURE T_TEST
IS
V_SQL NVARCHAR2(2000);
CURSOR CURSOR_1 IS
SELECT DISTINCT PROD
FROM TB1
ORDER BY PROD;
BEGIN
V_SQL := 'SELECT CUST ';
FOR V_TB IN CURSOR_1
LOOP
V_SQL := V_SQL
------解决方案--------------------
','
------解决方案--------------------
'MAX(DECODE(T.PROD,'''
------解决方案--------------------
V_TB.PROD
------解决方案--------------------
''',T.PROD,NULL)) AS PROD_'
------解决方案--------------------
V_TB.PROD
------解决方案--------------------
'';
END LOOP;
V_SQL := V_SQL
------解决方案--------------------
' FROM TB1 T' ;
V_SQL := V_SQL
------解决方案--------------------
' GROUP BY CUST';
DBMS_OUTPUT.PUT_LINE(V_SQL);