oracle 存储过程-动态行转列,解决。
包头
create or replace package pro_test as
TYPE out_cursor IS REF CURSOR;
procedure Alarm_ContentsByTime(
p_StartTime varchar2, ----开始时间
p_EndTime varchar2, ----结束时间
io_cursor in OUT out_cursor
); end pro_test;
包体
create or replace package body pro_test is procedure Alarm_ContentsByTime(
p_StartTime varchar2, ----开始时间
p_EndTime varchar2, ----结束时间
io_cursor in OUT out_cursor
) is
cursor cursor_1 IS
--获取 列用于循环。
select DISTINCT t.ps_productorcode code, t.PS_PRODUCTORNAME name,1 type FROM PSINFO_PRODUCTYIELD_SB t
union all
select DISTINCT f.ps_rawcode code,f.ps_rawname name,2 type from PSINFO_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 (
select
c.c0003_stcode,
c.c0003_stname,
a.ps_productorname ps_name,
a.ps_unit,
sum(nvl(a.ps_productoryield,0)) cl
from t0003_station c,PSINFO_PRODUCTYIELD_SB a
where 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 all
select
c.c0003_stcode,
c.c0003_stname,
b.ps_rawname ps_name,
b.ps_unit,
sum(nvl(b.ps_productoryield,0)) cl
from t0003_station c,PSINFO_RAWMATERIALS_SB b
where 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_cursor for V_SQL;
end Alarm_ContentsByTime;
end pro_test;
执行之后

链接地址:http://www.cnblogs.com/blue123/p/8441112.html
oracle 存储过程-动态行转列,解决。的更多相关文章
- 中等难度SQL语句(存储过程,分页,拼接字段、游标,日期类型转换,动态行转列,视图)汇总
一.创建存储过程 if Exists(select name from sysobjects where NAME = 'sp1LoginUser' and type='P')drop procedu ...
- 【转】MySQL存储过程中使用动态行转列
MySQL存储过程中使用动态行转列 最近做项目关于数据报表处理,然而数据库存储格式和报表展现形式不同,需要进行一下行转列的操作,在做上一个项目的时候也看了一下,但是后来换了读取方式,也就没深入研究这个 ...
- MySQL存储过程的动态行转列
MySQL存储过程中使用动态行转列 最近做项目关于数据报表处理,然而数据库存储格式和报表展现形式不同,需要进行一下行转列的操作,在做上一个项目的时候也看了一下,但是后来换了读取方式,也就没深入研究这个 ...
- [MSSQL]採用pivot函数实现动态行转列
环境要求:2005+ 在日常需求中常常会有行转列的事情需求处理.假设不是动态的行,那么我们能够採取case when 罗列处理. 在sql 2005曾经处理动态行或列的时候,通常採用拼接字符串的方法处 ...
- MySQL 行转列 -》动态行转列 -》动态行转列带计算
Pivot Table Using MySQL - A Complete Guide | WebDevZoomhttp://webdevzoom.com/pivot-table-using-mysql ...
- 在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)
原文:在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉 ...
- Oracle存储过程动态创建临时表/存储过程执行权限问题--AUTHID CURRENT_USER
关于Oracle存储过程执行权限问题的解决 http://blog.sina.com.cn/s/blog_6ceed3280101hvlo.html (2014-04-02 04:06:28) 转载▼ ...
- mybatis 动态行转列
SELECT user_name , MAX(CASE course WHEN '数学' THEN score END ) 数学, MAX(CASE course WHEN '语文' THEN sco ...
- 在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列)
原文:在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
- 在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名)
原文:在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
随机推荐
- Advanced Rest Client 测试工具安装
浏览器安装测试工具: Advanced Rest Client 1.下载插件,下载后解压 链接:https://pan.baidu.com/s/1DD5PgmkX_AG-JhUh8EVtqQ?pwd= ...
- linux办公
办公环境.生产环境: centos.redhat.big cloud 1.改PS1:生产主机的主机名太长了,每次显示占半行 [root@10.0.0.11 ~]$ echo $PS1 [\u@10.0 ...
- 升级grafana
We recommend everyone to upgrade Grafana often to stay up to date with the latest fixes and enhancem ...
- Java编写1到100质数之和
int sum = 0; int k = 2; // 找出1-100的质数之和 for (int i = 2; i <= 100; i++) { // i值为2,质数为除去1和自身整除的数 j初 ...
- jmeter组件
1.进程:一个正在执行的程序就对应一个进程 线程:进程中的执行线索(一个进程有多个执行线索) 线程组:按照线程性质对线程进行分组 2.并发执行:多个线程同时执行 特点:执行结束的顺序和线程的启动顺序不 ...
- js检测数组是否有重复的数据,
function(arr){ let hash={} for (const key in arr){ if(hash[arr[key]]){ return true } hash[arr[key]]= ...
- nodeJs 写个爬虫小玩意
内容 起一个服务,爬某个网站的数据(我这里爬了个夕阳红游戏交易网站的数据),页面看到我要爬的内容 代码 1 //引入内置的http包 2 var http = require('http'); 3 c ...
- 内存模型--共享、JMM
Balking 指令重排
- 00_k8s_learn
10本 Kubernetes 学习书籍推荐 - 知乎 https://zhuanlan.zhihu.com/p/468291559 Helm 的安装使用 · 从 Docker 到 Kubernetes ...
- 详解搭建ubuntu版hadoop集群
https://download.csdn.net/download/weixin_38583278/12844195?ops_request_misc=%257B%2522request%255Fi ...