sqlyear:=sqlyear+1; exit when sqlyear>TempYearEnd;
end loop;
open io_cursor for
'select * from( select * from (select p.xmmc,p.jsgm,p.xyzb,p.pftz,'||TempAddTempSumSelz||','||TempAddTempSumSeld||','||TempAddTempSumSelx||' '||TempSumSel||',p.szss,p.yjlxid--把所有的项目选出求和 from (select xmid'||TempSum||' from (select xmid '||sqlin||' from annualinvest a) group by xmid) n,project p where n.xmid=p.id union --把所有的项目按建设位置求和选出求和 select n.szss , null , null,n.pftz ,'||TempAddTempTypeSumSelz||','||TempAddTempTypeSumSeld||','||TempAddTempTypeSumSelx||''||TempTypeSumSel||' , n.szss,null yjlxid from (select szss,sum(pftz) pftz'||TempTypeSum||' from (select n.*,p.* from (select xmid'||TempSum||' from (select xmid '||sqlin||' from annualinvest a) group by xmid) n,project p where n.xmid=p.id ) group by szss) n union--把所有的项目按一级类型求和选出求和 select pt.yjlxmc , null , null,n.pftz ,'||TempAddTempTypeSumSelz||','||TempAddTempTypeSumSeld||','||TempAddTempTypeSumSelx||''||TempTypeSumSel||',n.szss,n.yjlxid from (select yjlxid,szss,sum(pftz) pftz '||TempTypeSum||' from (select n.*,p.* from (select xmid'||TempSum||' from (select xmid '||sqlin||' from annualinvest a) group by xmid) n,project p where n.xmid=p.id ) group by szss,yjlxid) n,PROJECTTYPE pt where pt.id=n.yjlxid union--求所有项目的合计 select xmmc,jsgm,xyzb,pftz,'||TempAddTempTypeSumSelz||','||TempAddTempTypeSumSeld||','||TempAddTempTypeSumSelx||''||TempTypeSumSel||' ,szss,yjlxid from ( select ''合计'' xmmc, null jsgm, sum(pftz) pftz, null xyzb, null szss,null yjlxid'||TempTypeSum||' from (select n.*, p.pftz from (select xmid '||TempSum||' from (select xmid '||sqlin||' from annualinvest a) group by xmid) n,project p where n.xmid=p.id)) n) order by szss desc,yjlxid desc,jsgm desc)--把所有记录按gcwzid,yjlxid的降序排列 where '||SqlSelin;
(io_cursor out t_cursor,--把结果以游标的形式返回
SqlSelin in varchar2,--查询条件
yearstart in varchar2,--查询起始年
yearend in varchar2)--查询结束年
is
sqlyear varchar2(10);--查询表中的年度为sqlyear的相应记录
sqlin varchar2(1000); ---把项目年度投资的记录竖变横
TempSum varchar2(1000);--把sqlin产生的记录按相同项目进行求和
TempYearEnd number;--临时存储结束年,以便作LOOP结束判断的年份
TempTypeSum varchar2(1000); ---对同类的项目进行求和
TempSumSel varchar2(1000);--在TempSum外边的查询把TempSum产生的记录相应列选出
TempTypeSumSel varchar2(1000);---在TempTypeSum外边的查询把TempTypeSum产生的记录相应列选出
TempAddTempSumSelz varchar2(1000);
TempAddTempSumSeld varchar2(1000);
TempAddTempSumSelx varchar2(1000);
TempAddTempTypeSumSelz varchar2(1000);
TempAddTempTypeSumSeld varchar2(1000);
TempAddTempTypeSumSelx varchar2(1000);begin
TempAddTempSumSelz:='0';
TempAddTempSumSeld:='0';
TempAddTempSumSelx:='0';
TempAddTempTypeSumSelz:='0';
TempAddTempTypeSumSeld:='0';
TempAddTempTypeSumSelx:='0';
sqlyear:=to_number(yearstart,'9999');
TempYearEnd:=to_number(yearend,'9999'); loop--当年份在yearstart,yearend之间的时候进行循环,年份循环一次加一,直至超过yearend
---把传进所有年份的项目年度投资的记录竖变横
sqlin:=sqlin||',nvl(decode(a.tznf,'||sqlyear||',a.zytz),0) z'||sqlyear||',
nvl(decode(a.tznf,'||sqlyear||',a.dftz),0) d'||sqlyear||',
nvl(decode(a.tznf,'||sqlyear||',a.xjtz),0) x'||sqlyear;
--把传进所有年份的sqlin产生的记录按相同项目进行求和
TempSum:=TempSum||',sum(z'||sqlyear||') sumz'||sqlyear||',
sum(d'||sqlyear||') sumd'||sqlyear||',
sum(x'||sqlyear||') sumx'||sqlyear;
--在TempSum外边的查询把TempSum产生的记录相应列选出
TempSumSel:=TempSumSel||',n.sumz'||sqlyear||',
n.sumd'||sqlyear||',
n.sumx'||sqlyear;
TempAddTempSumSelz:=TempAddTempSumSelz||'+n.sumz'||sqlyear;
TempAddTempSumSeld:=TempAddTempSumSeld||'+n.sumd'||sqlyear;
TempAddTempSumSelx:=TempAddTempSumSelx||'+n.sumx'||sqlyear;
---对同类的项目进行求和
TempTypeSum:=TempTypeSum||',sum(sumz'||sqlyear||') allz'||sqlyear||',
sum(sumd'||sqlyear||') alld'||sqlyear||',
sum(sumx'||sqlyear||') allx'||sqlyear;
---在TempTypeSum外边的查询把TempTypeSum产生的记录相应列选出
TempTypeSumSel:=TempTypeSumSel||',n.allz'||sqlyear||',
n.alld'||sqlyear||',
n.allx'||sqlyear;
TempAddTempTypeSumSelz:=TempAddTempTypeSumSelz||'+n.allz'||sqlyear;
TempAddTempTypeSumSeld:=TempAddTempTypeSumSeld||'+n.alld'||sqlyear;
TempAddTempTypeSumSelx:=TempAddTempTypeSumSelx||'+n.allx'||sqlyear;
sqlyear:=sqlyear+1;
exit when sqlyear>TempYearEnd;
end loop;
open io_cursor for
'select * from(
select * from
(select p.xmmc,p.jsgm,p.xyzb,p.pftz,'||TempAddTempSumSelz||','||TempAddTempSumSeld||','||TempAddTempSumSelx||' '||TempSumSel||',p.szss,p.yjlxid--把所有的项目选出求和
from (select xmid'||TempSum||' from
(select xmid '||sqlin||' from annualinvest a)
group by xmid) n,project p
where n.xmid=p.id
union --把所有的项目按建设位置求和选出求和
select n.szss , null , null,n.pftz ,'||TempAddTempTypeSumSelz||','||TempAddTempTypeSumSeld||','||TempAddTempTypeSumSelx||''||TempTypeSumSel||' , n.szss,null yjlxid
from
(select szss,sum(pftz) pftz'||TempTypeSum||' from
(select n.*,p.* from
(select xmid'||TempSum||' from
(select xmid '||sqlin||' from annualinvest a)
group by xmid) n,project p
where n.xmid=p.id )
group by szss) n
union--把所有的项目按一级类型求和选出求和
select pt.yjlxmc , null , null,n.pftz ,'||TempAddTempTypeSumSelz||','||TempAddTempTypeSumSeld||','||TempAddTempTypeSumSelx||''||TempTypeSumSel||',n.szss,n.yjlxid
from
(select yjlxid,szss,sum(pftz) pftz '||TempTypeSum||' from
(select n.*,p.* from
(select xmid'||TempSum||' from
(select xmid '||sqlin||' from annualinvest a)
group by xmid) n,project p
where n.xmid=p.id )
group by szss,yjlxid) n,PROJECTTYPE pt
where pt.id=n.yjlxid
union--求所有项目的合计
select xmmc,jsgm,xyzb,pftz,'||TempAddTempTypeSumSelz||','||TempAddTempTypeSumSeld||','||TempAddTempTypeSumSelx||''||TempTypeSumSel||' ,szss,yjlxid
from (
select ''合计'' xmmc, null jsgm, sum(pftz) pftz, null xyzb, null szss,null yjlxid'||TempTypeSum||'
from
(select n.*, p.pftz from
(select xmid '||TempSum||' from
(select xmid '||sqlin||' from annualinvest a)
group by xmid) n,project p
where n.xmid=p.id)) n)
order by szss desc,yjlxid desc,jsgm desc)--把所有记录按gcwzid,yjlxid的降序排列
where '||SqlSelin;
end ANNUALINVEST;
这只是我的猜测而已。