首先,如果您将来给我们您的数据库版本会更好。例如,如果您有 Oracle 11g,即使它不是您想要的,您也可以使用PIVOT做类似的事情
with pivot_data as (select rownum as no,emp_name,job_name from (
select 'Joe' as emp_name,'Sales' as job_name from dual union all
select 'Steve','Sales' from dual union all
select 'Paul','Exec' from dual union all
select 'Pete','Manager' from dual union all
select 'John','Exec' from dual union all
select 'Roger','Sec' from dual union all
select 'Scott','Exec' from dual))
select "'Sales'" as Sales,"'Exec'" as Exec,"'Sec'" as Sec,"'Manager'" as Manager
from pivot_data
pivot(max(emp_name) for job_name in ('Sales','Exec','Sec','Manager'))
SALES EXEC SEC MANAGER
-------------------------------
Roger
Scott
John
Joe
Steve
Paul
Pete
或者,如果这对您来说还不够,那么这个 PL/SQL 可以满足您的需求。我在 11g 上对其进行了测试,并且我相当肯定它可以与 10g 和 9i 一起使用,但我不能保证 DB2 或 PostGre 的任何内容:
declare
cursor C1 is
select 'Joe' as emp_name,'Sales' as job_name from dual union all
select 'Steve','Sales' from dual union all
select 'Paul','Exec' from dual union all
select 'Pete','Manager' from dual union all
select 'John','Exec' from dual union all
select 'Roger','Sec' from dual union all
select 'Scott','Exec' from dual;
varray_sales dbms_sql.varchar2_table;
varray_exec dbms_sql.varchar2_table;
varray_manager dbms_sql.varchar2_table;
varray_sec dbms_sql.varchar2_table;
i_sales number := 0;
i_exec number := 0;
i_manager number := 0;
i_sec number := 0;
n_line_aextr number;
colwidth number := 30;
line varchar2(120);
function max_de_deux (i number,x number) return number is
begin
return case when i > x then i else x end;
end;
begin
dbms_output.enable(120);
for l1 in c1 loop
if L1.job_name = 'Sales' then
i_sales := i_sales+1;
varray_sales(i_sales) := L1.emp_name;
elsif L1.job_name = 'Exec' then
i_exec := i_exec+1;
varray_exec(i_exec) := L1.emp_name;
elsif L1.job_name = 'Manager' then
i_Manager := i_Manager+1;
varray_manager(i_Manager) := L1.emp_name;
elsif L1.job_name = 'Sec' then
i_sec := i_sec+1;
varray_sec(i_sec) := L1.emp_name;
end if;
end loop;
dbms_output.put_line(rpad('Sales',colwidth,' ')||rpad('Exec',colwidth,' ')||rpad('Manager',colwidth,' ')
||rpad('Sec',colwidth,' '));
dbms_output.put_line(rpad('-',colwidth,'-')||rpad('-',colwidth,'-')||rpad('-',colwidth,'-')
||rpad('-',colwidth,'-'));
n_line_aextr := max_de_deux(i_manager,max_de_deux(i_sec,max_de_deux(i_sales,i_exec)));
for l in 1..n_line_aextr loop
line := '';
if l <= i_sales then
line := line || rpad(varray_sales(l),colwidth,' ');
else
line := line || rpad(' ',colwidth,' ');
end if;
if l <= i_exec then
line := line || rpad(varray_exec(l),colwidth,' ');
else
line := line || rpad(' ',colwidth,' ');
end if;
if l <= i_manager then
line := line || rpad(varray_manager(l),colwidth,' ');
else
line := line || rpad(' ',colwidth,' ');
end if;
if l <= i_sec then
line := line || rpad(varray_sec(l),colwidth,' ');
else
line := line || rpad(' ',colwidth,' ');
end if;
dbms_output.put_line(line);
end loop;
end;
Sales Exec Manager Sec
------------------------------------------------------------
Joe Paul Pete Roger
Steve John
Scott