GreenPlum之按月份或季度实现行转列
来源:互联网 发布:ubuntu 笔记本 编辑:程序博客网 时间:2024/06/07 13:07
1.参考GreenPlum之生成月份字典表
GreenPlum之生成月份字典表
2.根据Case When语句及GreenPlum中string_agg聚合函数拼凑对应月份或季度周期字段,以下是核心实现sql,读者可根据需求自行修改:
--字段按照月份行转列
if position('yyyy-mm' in in_datecolum)>0 then out_strsql := ' select '||case in_matrix when 1 then 'string_agg('',cast(count(distinct case when 日期字段=''''''||monthid||'''''' then 表名.字段名1 end) as numeric(16,0)) as "''||monthid||''" '','''' order by monthid)' when 2 then 'string_agg('',sum(case when 日期字段=''''''||monthid||'''''' then 表名.字段名2 else 0 end) as "''||monthid||''" '','''' order by monthid)' when 3 then 'string_agg('',sum(case when 日期字段=''''''||monthid||'''''' then 表名.字段名3 else 0 end) as "''||monthid||''" '','''' order by monthid)' when 4 then 'string_agg('',cast(count(distinct case when 日期字段=''''''||monthid||'''''' then 表名.字段名4 end) as numeric(16,0)) as "''||monthid||''" '','''' order by monthid)' else 'string_agg('',sum(case when 日期字段=''''''||monthid||'''''' then 表名.字段名3 else 0 end) as "''||monthid||''" '','''' order by monthid)' end ||'from ( select monthid from monthmapping where onday between '''||in_startdate||''' and '''||in_overdate||''' group by monthid) tab '; execute out_strsql into aggstr;
--字段按照季度实现行转列 else out_strsql := ' select '||case in_matrix when 1 then 'string_agg('',cast(count(distinct case when 日期字段=''''''||monthid||'''''' then 表名.字段名1 end) as numeric(16,0)) as "''||monthid||''" '','''' order by monthid)' when 2 then 'string_agg('',sum(case when 日期字段=''''''||monthid||'''''' then 表名.字段名2 else 0 end) as "''||monthid||''" '','''' order by monthid)' when 3 then 'string_agg('',sum(case when 日期字段=''''''||monthid||'''''' then 表名.字段名3 else 0 end) as "''||monthid||''" '','''' order by monthid)' when 4 then 'string_agg('',cast(count(distinct case when 日期字段=''''''||monthid||'''''' then 表名.字段名4 end) as numeric(16,0)) as "''||monthid||''" '','''' order by monthid)' else 'string_agg('',sum(case when 日期字段=''''''||monthid||'''''' then 表名.字段名3 else 0 end) as "''||monthid||''" '','''' order by monthid)' end ||'from ( select substring(onday,1,4)||''-''||EXTRACT(QUARTER from onday)||''-01'' as monthid from monthmapping where onday between '''||in_startdate||''' and '''||in_overdate||''' group by substring(onday,1,4)||''-''||EXTRACT(QUARTER from onday)||''-01'') tab '; execute out_strsql into aggstr; end if; raise notice 'aggstr : %',aggstr;
阅读全文
1 0
- GreenPlum之按月份或季度实现行转列
- javascript实现日期按月份加减
- javascript实现日期按月份加减
- 按月按季度统计
- 按月份统计
- 按月份去旅游
- SQL 按月份查询报表
- 按月份打印日历函数
- oracle 查询按月份分组
- Oracle按月份累计求和
- 按月份分组查询例子
- laravel实现按月或天或小时统计mysql数据
- MySQL分表处理的实现方法(按月份分的表)
- 玩转EXCEL系列透视表实现按月份汇总数据
- sql查询单个项目的全年总和,按月份统计怎么实现?
- Linux之shell脚本编程实例--遍历所有文件,并按月份打成zip包
- 研发绩效考核按月还是按季度进行?
- Asp按月份自动创建文件夹函数
- 递归变递推,DP
- Android常用系统广播
- 单片机的定时器中断的学习(一)
- 冒泡排序
- Spring
- GreenPlum之按月份或季度实现行转列
- test命令的文件比较功能
- POJ 3104 Drying
- spring定时任务的配置及注释
- 5并行数据加载
- 二级指针输入模型(三种内存模型)
- POJ 1655 Balancing Act
- eclipse中使用git
- Java操作Excel基本