-----------------存储过程包体-----------

procedure GetComparativeAnalysisTB(p_StartTime varchar2, ----开始时间
p_EndTime varchar2, ----结束时间
p_type varchar2, ----1:按全市2:按行业3、按企业
p_id varchar2, ----1全部 、区县编码或企业名称
p_id1 varchar2, ----按企业查询p_id为区县p_id1为企业编码
c_Select in out REF_WFData)
is
cursor cursor_1 IS
---获取 时间段年份
SELECT (to_number(to_char(TO_DATE(p_StartTime, 'yyyy-MM'), 'YYYY')) + ROWNUM-1) AS Yearlist
FROM DUAL
CONNECT BY ROWNUM <= (to_char(to_date(p_EndTime, 'yyyy-MM'),'yyyy')-to_char(to_date(p_StartTime, 'yyyy-MM'),'yyyy'))+1
order by Yearlist desc;
V_SQL VARCHAR2(32767);
/* aYear varchar2(200);---获取年*/
bMonth varchar2(200);---获取月
v_StartTime varchar2(200); ---- 开始时间
v_EndTime varchar2(200); ----结束时间
begin
v_StartTime:=p_StartTime||'-01';
v_EndTime:=p_EndTime||'-01';
if p_type=1 then ---统计类别---- 按全市
V_SQL := 'select t1.ps_code,t1.ps_name';
FOR V_XCLCK IN cursor_1
LOOP
SELECT to_char(substr(p_StartTime,6,instr(p_StartTime,'-')-3)) into bMonth FROM DUAL ;--获取月
V_SQL := V_SQL ||','|| 'min(case when to_char(t1.ps_month,''yyyy-mm'')=''' || V_XCLCK.Yearlist||'-'|| bMonth||''' then t1.pfl end) as ' || 'pfl'||V_XCLCK.Yearlist||bMonth||','
|| 'min(case when to_char(t1.ps_month,''yyyy-mm'')=''' || V_XCLCK.Yearlist||'-'|| bMonth||''' then t1.fqpfl end) as ' || 'fqpfl'||V_XCLCK.Yearlist||bMonth;
END LOOP;
V_SQL := V_SQL || ' from (
select
a.ps_month,
a.c0008_pid ps_code,
a.c0008_item_desc ps_name,
b.ps_pfl fqpfl,
sum(nvl(a.ps_pfl,0)) pfl
from PSINFO_OUTPUTSUBPFYZ_SB a,PSINFO_OUTPUTSUB_SB b
where a.c0003_stcode=b.c0003_stcode
and a.c0070_enterprise_code=b.c0070_enterprise_code
and a.c0007_pcode=b.c0007_pcode
and a.ps_month=b.ps_month
and a.ps_month>=to_date('''||v_StartTime||''',''yyyy-mm-dd'')
and a.ps_month<=to_date('''||v_EndTime||''',''yyyy-mm-dd'')
and a.c0008_pid in (201,203,207)
and a.ps_state=1
group by a.c0008_pid,a.c0008_item_desc,a.ps_month,b.ps_pfl
) t1
group by ps_code,ps_name
order by ps_code';
DBMS_OUTPUT.PUT_LINE(V_SQL);
open c_Select for V_SQL;
end if;
end GetComparativeAnalysisTB;

效果图:确定好年份,就会动态循环列头。

————————————————————动态月份列,效果图:显示到具体月份——————————————————————————

cursor cursor_1 IS
---获取 时间段月份
SELECT TO_CHAR(ADD_MONTHS(TO_DATE(p_StartTime, 'yyyy-MM'), ROWNUM - 1), 'yyyy-MM') as monthlist
FROM DUAL
CONNECT BY ROWNUM <= months_between(to_date(p_EndTime, 'yyyy-MM'),to_date(p_StartTime, 'yyyy-MM')) + 1 ;
V_SQL VARCHAR2(32767);
aYear varchar2(200);---获取年
bMonth varchar2(200);---获取月
v_StartTime varchar2(200); ---- 开始时间
v_EndTime varchar2(200); ----结束时间
begin
v_StartTime:=p_StartTime||'-01';
v_EndTime:=p_EndTime||'-01';
if p_type=1 then ---统计类别---- 按全市
if p_id='0' then
V_SQL := 'select t1.ps_code,t1.ps_name';
FOR V_XCLCK IN cursor_1
LOOP
SELECT to_char(substr(V_XCLCK.monthlist,1,instr(V_XCLCK.monthlist,'-')-1)),to_char(substr(V_XCLCK.monthlist,6,instr(V_XCLCK.monthlist,'-')-3)) into aYear,bMonth FROM DUAL ;
V_SQL := V_SQL ||','|| 'nvl(min(case when to_char(t1.ps_month,''yyyy-mm'')=''' || V_XCLCK.monthlist||''' then t1.pfl end),0) as ' || 'pfl'||aYear||bMonth||','
|| 'nvl(min(case when to_char(t1.ps_month,''yyyy-mm'')=''' || V_XCLCK.monthlist||''' then t1.fqpfl end),0) as ' || 'fqpfl'||aYear||bMonth;
END LOOP;
V_SQL := V_SQL || ' from (
select
a.ps_month,
a.c0008_pid ps_code,
a.c0008_item_desc ps_name,
b.ps_pfl fqpfl,
sum(nvl(a.ps_pfl,0)) pfl
from PSINFO_OUTPUTSUBPFYZ_SB a,PSINFO_OUTPUTSUB_SB b
where a.c0003_stcode=b.c0003_stcode
and a.c0070_enterprise_code=b.c0070_enterprise_code
and a.c0007_pcode=b.c0007_pcode
and a.ps_month=b.ps_month
and a.ps_month>=to_date('''||v_StartTime||''',''yyyy-mm-dd'')
and a.ps_month<=to_date('''||v_EndTime||''',''yyyy-mm-dd'')
and a.c0008_pid in (201,203,207)
and a.ps_state=1
group by a.c0008_pid,a.c0008_item_desc,a.ps_month,b.ps_pfl
) t1
group by ps_code,ps_name
order by ps_code';
DBMS_OUTPUT.PUT_LINE(V_SQL);
open c_Select for V_SQL;

oracle 行转列,动态年份,月份列。已解决!的更多相关文章

  1. Oracle 行转列 动态出转换的列

    本文链接:https://blog.csdn.net/Huay_Li/article/details/82924443 10月的第二天,前天写了个Oracle中行转列的pivot的基本使用方法,然后, ...

  2. Oracle 行转列pivot 、列转行unpivot 的Sql语句总结

    这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from ap ...

  3. Oracle行转列、列转行的Sql语句总结

    多行转字符串 这个比较简单,用||或concat函数可以实现  SQL Code  12    select concat(id,username) str from app_userselect i ...

  4. Oracle行转列、列转行的Sql语句总结(转)

    多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str f ...

  5. oracle 行转列 分析函数

    oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + || ':' ...

  6. oracle行转列,列转行

    多行转字符串这个比较简单,用||或concat函数可以实现 SQL Code select concat(id,username) str from app_userselect id||userna ...

  7. 在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)

    原文:在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉 ...

  8. Oracle行转列,pivot函数和unpivot函数

    pivot函数:行转列函数: 语法:pivot(任一聚合函数 for 需专列的值所在列名 in (需转为列名的值)):unpivot函数:列转行函数: 语法:unpivot(新增值所在列的列名 for ...

  9. Oracle 行转列小结

    近期在工作中.对行转列进行了应用,在此做一个简单的小结. 转换步骤例如以下:     1.创建表结构 CREATE TABLE RowToCol ( ID NUMBER(10) not null, U ...

  10. Oracle行转列,列转行,行列相互转换

    1.行转列 SELECT WM_CONCAT(COLUMN_NAME) COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'T_CREATE_T ...

随机推荐

  1. (5)go-micro微服务domain层开发

    目录 一 domain层介绍说明 二 model层开发 三 repository层开发 四 service层开发 五 最后 一 domain层介绍说明 domain层专注于数据库数据领域开发,我们把数 ...

  2. [cocos2d-x]用getContentSize()返回的值用CCLOG打印必须用%f

    今天写代码,又遇到了一个bug,开始还以为是我自己哪写错了,没想到竟然在这里出错? 而width和height的类型是float类型,为什么必须用%d打印?接着我查看了一下源码: 如果没理解错的话,C ...

  3. 【Basic Knowledge】Self-Attention Generative Adversarial Networks

    Note   这是一篇将Self-Attention应用到GAN中的paper,Self-Attention模块是卷积模块的补充,能够有助于建模跨图像区域的长范围.多层次依赖关系.文中主要提到4点: ...

  4. 保姆级教程-分分钟掌握常用TS语法

    十分钟就搞懂TS用法 先全局安装一些工具,有助于我们编写运行代码哦 1.VScode插件 code runner 2.安装依赖 npm install typescript ts-node -g 开始 ...

  5. vue基础之keep-alvie保持历史页面数据不变,切换页面后数据不变keep-alvie

    1:路由配置文件设置 { path: '/MenuM', component: Layout, redirect: '/MenuM', children: [ { path: 'MenuM', nam ...

  6. Vue插槽最全最通俗的总结

    1.匿名插槽----就是定义的时候不带name的插槽,使用的时候也不用带name 总结: (1)一定要分清定义插槽和使用插槽格式.如上图A是在使用一个插槽的基础上再继续定义一个匿名插槽. (2)匿名插 ...

  7. Unity之GPS定位(腾讯sdk)

    Unity之GPS定位(腾讯sdk) 目录 Unity之GPS定位(腾讯sdk) 前言 Unity版本及使用插件 正题 编写脚本 Run运行, 跑起来就行,具体要什么,去相关类找就好了. 没有腾讯地图 ...

  8. JAVA虚拟机10---内存分配策略

    1.简介 对象的内存分配,从概念上讲,应该都是在堆上分配(而实际上也有可能经过即时编译后被拆散为标量类型并间接地在栈上分配).在经典分代的设计下,新生对象通常会分配在新生代中,少数情况下(例如对象大小 ...

  9. vivo官网App模块化开发方案-ModularDevTool

    作者:vivo 互联网客户端团队- Wang Zhenyu 本文主要讲述了Android客户端模块化开发的痛点及解决方案,详细讲解了方案的实现思路和具体实现方法. 说明:本工具基于vivo互联网客户端 ...

  10. 图论之最短路径Dijkstra算法

    /** 图论之最短路径Dijkstra算法 */ #include<string.h> #include<stdio.h> #include<vector> #in ...