17,086
社区成员
发帖
与我相关
我的任务
分享
usedate chinaoil exciusoil excichoil wtoil
2009-08-22 1 2 3 4
2009-08-23 9 10 11 12
2009-08-25 13 14 15 16
2009-08-22 2009-08-23 2009-08-25
chinaoil 1 9 13
exciusoil 2 10 14
excichoil 3 11 15
wtoil 4 12 16
declare
strSql varchar2(2000);
cursor datelist is select usedate from a;
strDate varchar2(20);
begin
strSql:='select num';
open datelist;
loop
fetch datelist into strDate;
exit when datelist%notfound;
strSql:=strSql||',sum(decode(usedate, '''||strDate||''', cnt,0)) "'||strDate||'"';
end loop;
close datelist;
strSql:=strSql||' from (
select usedate,1 num,chinaoil cnt from a
union select usedate,2 num,exciusoil cnt from a
union select usedate,3 num,excichoil cnt from a
union select usedate,4 num,wtoil cnt from a)
group by num order by num asc';
dbms_output.put_line(strSql);
end;
declare
sqlstr varchar2(2000):='select title';
begin
for cur1 in (select * from a)
loop
sqlstr:=sqlstr||'
,max(decode(usedate,'''||cur1.usedate||''',cnt))"'||cur1.usedate||'"';
end loop;
sqlstr:=sqlstr||'
from (select usedate,''chinaoil'' title,chinaoil cnt from a union all
select usedate,''exciusoil'',exciusoil from a union all
select usedate,''excichoil'',excichoil from a union all
select usedate,''wtoil'',wtoil from a)
group by title';
dbms_output.put_line(sqlstr);
end;
select max(decode(usedate,'2009-08-22',chinaoil))"2009-08-22",
max(decode(usedate,'2009-08-23',chinaoil))"2009-08-23",
max(decode(usedate,'2009-08-25',chinaoil))"2009-08-25" from a union all
select max(decode(usedate,'2009-08-22',exciusoil)),
max(decode(usedate,'2009-08-23',exciusoil)),
max(decode(usedate,'2009-08-25',exciusoil)) from a union all
select max(decode(usedate,'2009-08-22',excichoil)),
max(decode(usedate,'2009-08-23',excichoil)),
max(decode(usedate,'2009-08-25',excichoil)) from a union all
select max(decode(usedate,'2009-08-22',wtoil)),
max(decode(usedate,'2009-08-23',wtoil)),
max(decode(usedate,'2009-08-25',wtoil)) from a