oracle 行转列,动态年份,月份列。已解决!
-----------------存储过程包体-----------
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 行转列,动态年份,月份列。已解决!的更多相关文章
- Oracle 行转列 动态出转换的列
本文链接:https://blog.csdn.net/Huay_Li/article/details/82924443 10月的第二天,前天写了个Oracle中行转列的pivot的基本使用方法,然后, ...
- Oracle 行转列pivot 、列转行unpivot 的Sql语句总结
这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from ap ...
- Oracle行转列、列转行的Sql语句总结
多行转字符串 这个比较简单,用||或concat函数可以实现 SQL Code 12 select concat(id,username) str from app_userselect i ...
- Oracle行转列、列转行的Sql语句总结(转)
多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str f ...
- oracle 行转列 分析函数
oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + || ':' ...
- oracle行转列,列转行
多行转字符串这个比较简单,用||或concat函数可以实现 SQL Code select concat(id,username) str from app_userselect id||userna ...
- 在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)
原文:在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉 ...
- Oracle行转列,pivot函数和unpivot函数
pivot函数:行转列函数: 语法:pivot(任一聚合函数 for 需专列的值所在列名 in (需转为列名的值)):unpivot函数:列转行函数: 语法:unpivot(新增值所在列的列名 for ...
- Oracle 行转列小结
近期在工作中.对行转列进行了应用,在此做一个简单的小结. 转换步骤例如以下: 1.创建表结构 CREATE TABLE RowToCol ( ID NUMBER(10) not null, U ...
- Oracle行转列,列转行,行列相互转换
1.行转列 SELECT WM_CONCAT(COLUMN_NAME) COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'T_CREATE_T ...
随机推荐
- 微软拼音长句模式恢复工具支持Win10 1803
4月份就有人留言旧微软拼音恢复工具不支持Win10 1803了,我自己也遇到了,但因为没时间搞,勉为其难使用了词组模式的微软拼音几个月,终于在八月份抽个空研究了下,解决了. 这次是因为傻逼大微软改了 ...
- [LeetCode]819. 最常见的单词
题目 给定一个段落 (paragraph) 和一个禁用单词列表 (banned).返回出现次数最多,同时不在禁用列表中的单词.题目保证至少有一个词不在禁用列表中,而且答案唯一. 禁用列表中的单词用小写 ...
- Embracing Domain Differences in Fake News- Cross-domain Fake News Detection using Multimodal Data(AAAI21)
一.摘要 随着社交媒体的快速发展,假新闻已经成为一个重大的社会问题,它无法通过人工调查及时解决.这激发了大量关于自动假新闻检测的研究. 大多数研究探索了基于新闻记录中不同模态信息(如文本.图像和传播网 ...
- 【Dubbo3终极特性】「流量治理体系」一文教你如何通过Dubbo-Admin实现动态进行流量隔离机制
背景信息 如果一个应用有多个版本在线上同时运行,部署在不同环境中,如日常环境和特殊环境,则 可以使用标签路由对不同环境中的不同版本进行流量隔离,将秒杀订单流量或不同渠道订单流量路由到特殊环境,将正常的 ...
- (20)go-micro微服务Elasticsearch使用
目录 一 Elasticsearch介绍 二 Elasticsearch的主要功能及应用场景 1.Elasticsearch 主要具有如下功能: 2.Elasticsearch 的主要应用场景如下: ...
- 【分析笔记】全志方案通过命令行操作 GPIO 口(带源码分析)
前言说明 在项目开发初期,很经常会需要临时操作某个GPIO来验证某些功能,可以通过编写一个简单的驱动程序来操作,但更方便的是可以通过命令行直接操作 GPIO ,这样不需要经过编写代码.编译驱动.推入文 ...
- 软工综合实践课设——员工招聘系统(参考BOSS直聘);Pyhton实现
应用背景: 随着科学技术的发展,岗位数量越来越多,特别是每逢毕业季找工作的人数也很多,如果人们找工作或者企业招人靠纯手工的话,费时费力,仅仅是筛选简历和费劲,并且员工找工作投简历可能得需要克服时间和空 ...
- Grafana 系列文章(八):Grafana Explore 中的 Inspector
️URL: https://grafana.com/docs/grafana/latest/explore/explore-inspector/ Description: Explore 中的检查器 ...
- 10月11日内容总结——global和nonlocal方法、函数名的多种用法、闭包函数和装饰器
目录 一.global和nonlocal方法 global方法 nonlocal方法 二.函数名的多种用法 1.函数名可以当作变量名赋值 2.函数名可以当作函数的参数 3.函数名可以当作函数的返回值 ...
- Pytest插件pytest-order指定用例顺序
Pytest插件pytest-order指定用例顺序 安装 pip install pytest-order 注意不是pytest-ordering 说起来这里有个故事 关于pytest-order ...