oracle存储过程行转列,oracle 存储过程-动态行转列,解决。

create or replace package body pro_test isprocedure Alarm_ContentsByTime(

p_StartTime varchar2,----开始时间

p_EndTime varchar2,----结束时间

io_cursorinOUT out_cursor

)iscursor cursor_1 IS--获取 列用于循环。select DISTINCT t.ps_productorcode code, t.PS_PRODUCTORNAME name,1type FROM PSINFO_PRODUCTYIELD_SB t

union allselect DISTINCT f.ps_rawcode code,f.ps_rawname name,2 type fromPSINFO_RAWMATERIALS_SB f

ORDER BY type,code;

V_SQL VARCHAR2(6000);

begin

V_SQL := ‘select‘‘市本级‘‘as c0003_stname, 1 as c0003_stcode‘;

FOR V_XCLCK IN cursor_1

LOOP

V_SQL := V_SQL || ‘,‘ || ‘min(case when t1.ps_name=‘‘‘ || V_XCLCK.name ||

‘‘‘then t1.ps_name end) as‘ || V_XCLCK.name||‘mc‘|| ‘,‘ || ‘min(case when t1.ps_name=‘‘‘ || V_XCLCK.name ||

‘‘‘then t1.cl end) as‘ || V_XCLCK.name||‘cl‘|| ‘,‘ || ‘min(case when t1.ps_name=‘‘‘ || V_XCLCK.name ||

‘‘‘then t1.ps_unit end) as‘ || V_XCLCK.name||‘dw‘;END LOOP;

V_SQL := V_SQL || ‘from t0003_station t

inner join (selectc.c0003_stcode,

c.c0003_stname,

a.ps_productorname ps_name,

a.ps_unit,

sum(nvl(a.ps_productoryield,0)) clfromt0003_station c,PSINFO_PRODUCTYIELD_SB awhere a.c0003_stcode=c.c0003_stcode

and a.ps_month>=to_date(‘‘‘||p_StartTime||‘‘‘,‘‘yyyy-mm-dd hh24:mi:ss‘‘)

and a.ps_month<=to_date(‘‘‘||p_EndTime||‘‘‘,‘‘yyyy-mm-dd hh24:mi:ss‘‘)

group by c.c0003_stcode,c.c0003_stname,a.ps_productorname,a.ps_unit

union allselectc.c0003_stcode,

c.c0003_stname,

b.ps_rawname ps_name,

b.ps_unit,

sum(nvl(b.ps_productoryield,0)) clfromt0003_station c,PSINFO_RAWMATERIALS_SB bwhere b.c0003_stcode=c.c0003_stcode

and b.ps_month>=to_date(‘‘‘||p_StartTime||‘‘‘,‘‘yyyy-mm-dd hh24:mi:ss‘‘)

and b.ps_month<=to_date(‘‘‘||p_EndTime||‘‘‘,‘‘yyyy-mm-dd hh24:mi:ss‘‘)

group by c.c0003_stcode,c.c0003_stname,b.ps_rawname,b.ps_unit

) t1 on t.c0003_stcode=t1.c0003_stcode

left join t0070_enterprise t70 on

t.c0003_stcode=t70.c0003_stcode

and t70.c0003_year=t1.c0003_stcode

and t70.c0070_stcode_ctrl=‘‘320100‘‘group by t.c0003_stname,t.c0003_stcode‘;

DBMS_OUTPUT.PUT_LINE(V_SQL);--V_SQL := ‘CREATE OR REPLACE VIEW RESULT AS‘||V_SQL;

open io_cursorforV_SQL;

end Alarm_ContentsByTime;

end pro_test;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值