Oracle行转列,列中存放多个值
表结构和数据如下
insert into prd_slm.t_Slm_Supplier_Codereq (SUPPLIER_CODEREQ_ID, PID, SUPPLIER_PID, APPROVE_STATUS)
values (6, 'g3 ', 'a ', '6');
insert into prd_slm.t_Slm_Supplier_Codereq (SUPPLIER_CODEREQ_ID, PID, SUPPLIER_PID, APPROVE_STATUS)
values (1, 'g1 ', 'a ', '1');
insert into prd_slm.t_Slm_Supplier_Codereq (SUPPLIER_CODEREQ_ID, PID, SUPPLIER_PID, APPROVE_STATUS)
values (2, 'g1 ', 'b ', '2');
insert into prd_slm.t_Slm_Supplier_Codereq (SUPPLIER_CODEREQ_ID, PID, SUPPLIER_PID, APPROVE_STATUS)
values (3, 'g1 ', 'b ', '3');
insert into prd_slm.t_Slm_Supplier_Codereq (SUPPLIER_CODEREQ_ID, PID, SUPPLIER_PID, APPROVE_STATUS)
values (4, 'g2 ', 'b ', '4');
insert into prd_slm.t_Slm_Supplier_Codereq (SUPPLIER_CODEREQ_ID, PID, SUPPLIER_PID, APPROVE_STATUS)
values (5, 'g2 ', 'c ', '5');
WITH pivot_data AS(
SELECT pid, SUPPLIER_PID, APPROVE_STATUS
FROM prd_slm.t_Slm_Supplier_Codereq)
SELECT *
FROM pivot_data PIVOT( LISTAGG(approve_status, ',') WITHIN GROUP(ORDER BY SUPPLIER_PID)
--max(approve_status) --<-- pivot_clause
FOR supplier_pid --<-- pivot_for_clause
IN ('a','b','c','d') --<-- pivot_in_clause
);
values (6, 'g3 ', 'a ', '6');
insert into prd_slm.t_Slm_Supplier_Codereq (SUPPLIER_CODEREQ_ID, PID, SUPPLIER_PID, APPROVE_STATUS)
values (1, 'g1 ', 'a ', '1');
insert into prd_slm.t_Slm_Supplier_Codereq (SUPPLIER_CODEREQ_ID, PID, SUPPLIER_PID, APPROVE_STATUS)
values (2, 'g1 ', 'b ', '2');
insert into prd_slm.t_Slm_Supplier_Codereq (SUPPLIER_CODEREQ_ID, PID, SUPPLIER_PID, APPROVE_STATUS)
values (3, 'g1 ', 'b ', '3');
insert into prd_slm.t_Slm_Supplier_Codereq (SUPPLIER_CODEREQ_ID, PID, SUPPLIER_PID, APPROVE_STATUS)
values (4, 'g2 ', 'b ', '4');
insert into prd_slm.t_Slm_Supplier_Codereq (SUPPLIER_CODEREQ_ID, PID, SUPPLIER_PID, APPROVE_STATUS)
values (5, 'g2 ', 'c ', '5');
WITH pivot_data AS(
SELECT pid, SUPPLIER_PID, APPROVE_STATUS
FROM prd_slm.t_Slm_Supplier_Codereq)
SELECT *
FROM pivot_data PIVOT( LISTAGG(approve_status, ',') WITHIN GROUP(ORDER BY SUPPLIER_PID)
--max(approve_status) --<-- pivot_clause
FOR supplier_pid --<-- pivot_for_clause
IN ('a','b','c','d') --<-- pivot_in_clause
);