原先表:
id ele evalue
011 c 0.50
011 si 0.21
011 mn 0.25
011 s 0.25
012 c 0.24
012 mn 0.30
012 s 0.22
想得到如下结果:(ele 中的元素是动态改变的,根据id,ele生成横向
表)
id
c si mn s ...
011 0.50 0.21 0.250.25 ...
012 0.24 null 0.300.22 ...
仔细想了想,觉得用decode()函数可能不行。还是用pl/sql吧。下边是我收藏的itpub上的一篇帖子,你参考一下。
declare
cursor c1 is select a1,a2 from a order by a1;
v_a1 number := 0;
v_a2 varchar2(100) := null;
v_temp varchar2(100) := null;
v_lasta1 number := 0;
v_count number := 0;
begin
for r1 in c1 loop
v_a1 := r1.a1;
v_a2 := r1.a2;
v_count := v_count + 1;
if (v_count = 1) then
v_temp := v_a1 || ' '||v_a2;
else
if (v_a1 = v_lasta1) then
v_temp := v_temp || v_a2;
else
dbms_output.put_line(v_temp);
v_temp := v_a1 || ' '||v_a2;
end if;
end if;
v_lasta1 := v_a1;
end loop;
dbms_output.put_line(v_temp);
end;