1. -----------------存储过程包体-----------

  2. procedure GetComparativeAnalysisTB(p_StartTime varchar2, ----开始时间
  3. p_EndTime varchar2, ----结束时间
  4. p_type varchar2, ----1:按全市2:按行业3、按企业
  5. p_id varchar2, ----1全部 、区县编码或企业名称
  6. p_id1 varchar2, ----按企业查询p_id为区县p_id1为企业编码
  7. c_Select in out REF_WFData)
  8. is
  9. cursor cursor_1 IS
  10. ---获取 时间段年份
  11. SELECT (to_number(to_char(TO_DATE(p_StartTime, 'yyyy-MM'), 'YYYY')) + ROWNUM-1) AS Yearlist
  12. FROM DUAL
  13. CONNECT BY ROWNUM <= (to_char(to_date(p_EndTime, 'yyyy-MM'),'yyyy')-to_char(to_date(p_StartTime, 'yyyy-MM'),'yyyy'))+1
  14. order by Yearlist desc;
  15. V_SQL VARCHAR2(32767);
  16. /* aYear varchar2(200);---获取年*/
  17. bMonth varchar2(200);---获取月
  18. v_StartTime varchar2(200); ---- 开始时间
  19. v_EndTime varchar2(200); ----结束时间
  20. begin
  21. v_StartTime:=p_StartTime||'-01';
  22. v_EndTime:=p_EndTime||'-01';
  23. if p_type=1 then ---统计类别---- 按全市
  24. V_SQL := 'select t1.ps_code,t1.ps_name';
  25. FOR V_XCLCK IN cursor_1
  26. LOOP
  27. SELECT to_char(substr(p_StartTime,6,instr(p_StartTime,'-')-3)) into bMonth FROM DUAL ;--获取月
  28. 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||','
  29. || 'min(case when to_char(t1.ps_month,''yyyy-mm'')=''' || V_XCLCK.Yearlist||'-'|| bMonth||''' then t1.fqpfl end) as ' || 'fqpfl'||V_XCLCK.Yearlist||bMonth;
  30. END LOOP;
  31. V_SQL := V_SQL || ' from (
  32. select
  33. a.ps_month,
  34. a.c0008_pid ps_code,
  35. a.c0008_item_desc ps_name,
  36. b.ps_pfl fqpfl,
  37. sum(nvl(a.ps_pfl,0)) pfl
  38. from PSINFO_OUTPUTSUBPFYZ_SB a,PSINFO_OUTPUTSUB_SB b
  39. where a.c0003_stcode=b.c0003_stcode
  40. and a.c0070_enterprise_code=b.c0070_enterprise_code
  41. and a.c0007_pcode=b.c0007_pcode
  42. and a.ps_month=b.ps_month
  43. and a.ps_month>=to_date('''||v_StartTime||''',''yyyy-mm-dd'')
  44. and a.ps_month<=to_date('''||v_EndTime||''',''yyyy-mm-dd'')
  45. and a.c0008_pid in (201,203,207)
  46. and a.ps_state=1
  47. group by a.c0008_pid,a.c0008_item_desc,a.ps_month,b.ps_pfl
  48. ) t1
  49. group by ps_code,ps_name
  50. order by ps_code';
  51. DBMS_OUTPUT.PUT_LINE(V_SQL);
  52. open c_Select for V_SQL;
  53. end if;
  54. end GetComparativeAnalysisTB;

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

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

  1. cursor cursor_1 IS
  2. ---获取 时间段月份
  3. SELECT TO_CHAR(ADD_MONTHS(TO_DATE(p_StartTime, 'yyyy-MM'), ROWNUM - 1), 'yyyy-MM') as monthlist
  4. FROM DUAL
  5. CONNECT BY ROWNUM <= months_between(to_date(p_EndTime, 'yyyy-MM'),to_date(p_StartTime, 'yyyy-MM')) + 1 ;
  6. V_SQL VARCHAR2(32767);
  7. aYear varchar2(200);---获取年
  8. bMonth varchar2(200);---获取月
  9. v_StartTime varchar2(200); ---- 开始时间
  10. v_EndTime varchar2(200); ----结束时间
  11. begin
  12. v_StartTime:=p_StartTime||'-01';
  13. v_EndTime:=p_EndTime||'-01';
  14. if p_type=1 then ---统计类别---- 按全市
  15. if p_id='0' then
  16. V_SQL := 'select t1.ps_code,t1.ps_name';
  17. FOR V_XCLCK IN cursor_1
  18. LOOP
  19. 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 ;
  20. 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||','
  21. || 'nvl(min(case when to_char(t1.ps_month,''yyyy-mm'')=''' || V_XCLCK.monthlist||''' then t1.fqpfl end),0) as ' || 'fqpfl'||aYear||bMonth;
  22. END LOOP;
  23. V_SQL := V_SQL || ' from (
  24. select
  25. a.ps_month,
  26. a.c0008_pid ps_code,
  27. a.c0008_item_desc ps_name,
  28. b.ps_pfl fqpfl,
  29. sum(nvl(a.ps_pfl,0)) pfl
  30. from PSINFO_OUTPUTSUBPFYZ_SB a,PSINFO_OUTPUTSUB_SB b
  31. where a.c0003_stcode=b.c0003_stcode
  32. and a.c0070_enterprise_code=b.c0070_enterprise_code
  33. and a.c0007_pcode=b.c0007_pcode
  34. and a.ps_month=b.ps_month
  35. and a.ps_month>=to_date('''||v_StartTime||''',''yyyy-mm-dd'')
  36. and a.ps_month<=to_date('''||v_EndTime||''',''yyyy-mm-dd'')
  37. and a.c0008_pid in (201,203,207)
  38. and a.ps_state=1
  39. group by a.c0008_pid,a.c0008_item_desc,a.ps_month,b.ps_pfl
  40. ) t1
  41. group by ps_code,ps_name
  42. order by ps_code';
  43. DBMS_OUTPUT.PUT_LINE(V_SQL);
  44. 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. Svelte框架实现表格协同文档

    首先,从框架搭建上,本篇示例采用当下流行的前后端分离的开发方式,前端使用npm作为脚手架搭建Svelte框架. 后端使用Java的SpringBoot作为后端框架. 首先,介绍下在前端Svelte框架 ...

  2. 真正“搞”懂HTTP协议09之这个饼干不能吃

    我们在之前的文章中介绍HTTP特性的时候聊过,HTTP是无状态的,每次聊起HTTP特性的时候,我都会回忆一下从前辉煌的日子,也就是互联网变革的初期,那时候其实HTTP不需要有状态,就是个浏览页面,没有 ...

  3. 微机原理与系统设计笔记2 | 8086CPU结构与功能

    打算整理汇编语言与接口微机这方面的学习记录.本部分讲解8086CPU的结构和基本功能以及特性. 参考资料 西电<微机原理与系统设计>周佳社 西交<微机原理与接口技术> 课本&l ...

  4. Docker 基础 - 3

    Web 服务器与应用 Nginx 我的Nginx Docker镜像 ## 设置继承自己创建的 sshd 镜像 FROM caseycui/ubuntu-sshd ## 维护者 LABEL mainta ...

  5. 重磅!瞄准 Web 3.0,谷歌云推出专为区块链服务的 Blockchain Node Engine!

    [本文由Cloud Ace整理发布,谷歌云服务请访问Cloud Ace 官网] 区块链技术正在为世界各地的消费者和企业带来巨大的创新和价值创造.随着技术变得越来越主流,公司需要可扩展.安全和可持续的基 ...

  6. 复杂环境下ocr与印章识别技术理解及研发趋势

    引言 随着社会经济的发展,印章作为企事业单位.社会团体.政府部门乃至国家的一种具有法律意义的标志和证据,在现代社会生活中发挥着重要作用.随着现代商务活动的不断发展,企业在业务开展的过程中通常会涉及大量 ...

  7. DJI Flight Simulator 无人机模拟器 功能介绍与使用说明

    0 前言 无人机是当前非常火热的"相机设备",而大疆又是其中翘楚,功能丰富,可以说是一个将带着云台的智能手机放到了天空中.如果你有自己玩过旋翼无人机航模的话,可能会体会到大疆的另一 ...

  8. P34_数据请求 - GET和POST请求

    网络数据请求 小程序中网络数据请求的限制 出于安全性方面的考虑,小程序官方对数据接口的请求做出了如下两个限制: 只能请求 HTTPS 类型的接口 必须将接口的域名添加到信任列表中 配置 request ...

  9. STM32F4跳转函数

    JMP2APP void JMP2APP(void) { pFunction Jump_To_Application; uint32_t JumpAddress; if (((*(__IO uint3 ...

  10. 基于Vue3+TS的Monorepo前端项目架构设计与实现

    写在前面 你好,我是前端程序员鼓励师岩家兴!去年在另一个项目https://juejin.cn/post/7121736546000044046中,我向读者朋友们介绍了结合npm包管理工具yarn作v ...