zoukankan      html  css  js  c++  java
  • 根据提供的数据求出该日期所在的季度

    根据当前日期求字段中日期的季度 

    Oracle数据库

     1 select
     2 T1.INDEXCODE
     3 ,T1.CREATETIME 
     4 ,CASE when T1.CREATETIME = 
     5 (case when to_char(SYSDATE,'MM')/3-1 <= 0 
     6 then TRIM(to_char(SYSDATE,'YYYY')-1)||'-'||TRIM(4+(to_char(SYSDATE,'MM')/3-1))
     7 else TRIM(to_char(SYSDATE,'YYYY'))||'-'||TRIM(to_char(SYSDATE,'MM')/3-1)
     8 end) then '本季度' 
     9 when T1.CREATETIME = 
    10 (case when to_char(SYSDATE,'MM')/3-2 <= 0 
    11 then TRIM(to_char(SYSDATE,'YYYY')-1)||'-'||TRIM(4+(to_char(SYSDATE,'MM')/3-2))
    12 else TRIM(to_char(SYSDATE,'YYYY'))||'-'||TRIM(to_char(SYSDATE,'MM')/3-2)
    13 end)
    14 then '前1季度' 
    15 when T1.CREATETIME = 
    16 (case when to_char(SYSDATE,'MM')/3-3 <= 0 
    17 then TRIM(to_char(SYSDATE,'YYYY')-1)||'-'||TRIM(4+(to_char(SYSDATE,'MM')/3-3))
    18 else TRIM(to_char(SYSDATE,'YYYY'))||'-'||TRIM(to_char(SYSDATE,'MM')/3-3)
    19 end) 
    20 then '前2季度' 
    21 when T1.CREATETIME = 
    22 (case when to_char(SYSDATE,'MM')/3-4 <= 0 
    23 then TRIM(to_char(SYSDATE,'YYYY')-1)||'-'||TRIM(4+(to_char(SYSDATE,'MM')/3-4))
    24 else TRIM(to_char(SYSDATE,'YYYY'))||'-'||TRIM(to_char(SYSDATE,'MM')/3-4)
    25 end) 
    26 then '前3季度' 
    27 when T1.CREATETIME = 
    28 (case when to_char(SYSDATE,'MM')/3-5 <= 0 
    29 then TRIM(to_char(SYSDATE,'YYYY')-1)||'-'||TRIM(4+(to_char(SYSDATE,'MM')/3-5))
    30 else TRIM(to_char(SYSDATE,'YYYY'))||'-'||TRIM(to_char(SYSDATE,'MM')/3-5)
    31 end)
    32 then '前4季度' END as 指标
    33 from TableA  t1
    34 INNER JOIN TableB  t2 on T1.indexcode = t2.indexcode 
    35 where T2.indexcode in ('I1301000019')

    结果:

    在报表中使用动态参数,根据所选择日期信息展示所在季度各指标

     1 select  
     2 CREATETIME 
     3 ,CASE when T1.CREATETIME = '${jd}' then '本期' 
     4 when T1.CREATETIME = 
     5 (case when SUBSTR('${jd}',-1,1)-1<=0 
     6 then SUBSTR('${jd}',1,4)-1||'-'||(4+SUBSTR('${jd}',-1,1)-1)
     7 else SUBSTR('${jd}',1,4)||'-'||(SUBSTR('${jd}',-1,1)-1)
     8 end)
     9 then '前1期' 
    10 when T1.CREATETIME = 
    11 (case when SUBSTR('${jd}',-1,1)-2<=0 
    12 then SUBSTR('${jd}',1,4)-1||'-'||(4+SUBSTR('${jd}',-1,1)-2)
    13 else SUBSTR('${jd}',1,4)||'-'||(SUBSTR('${jd}',-1,1)-2)
    14 end) 
    15 then '前2期' 
    16 when T1.CREATETIME = 
    17 (case when SUBSTR('${jd}',-1,1)-3<=0 
    18 then SUBSTR('${jd}',1,4)-1||'-'||(4+SUBSTR('${jd}',-1,1)-3)
    19 else SUBSTR('${jd}',1,4)||'-'||(SUBSTR('${jd}',-1,1)-3)
    20 end) 
    21 then '前3期' 
    22 when T1.CREATETIME = 
    23 (case when SUBSTR('${jd}',-1,1)-4<=0 
    24 then SUBSTR('${jd}',1,4)-1||'-'||(4+SUBSTR('${jd}',-1,1)-4)
    25 else SUBSTR('${jd}',1,4)||'-'||(SUBSTR('${jd}',-1,1)-4)
    26 end)
    27 then '前4期' END as 季度
    28 from  table

    规范日期:将字符串形式的'2019-08-01'变成‘8/1’类型

    select replace(str(substring('2019-08-01',6,2)*1)+'/'+str(right('2019-08-01',2)*1),' ','')

  • 相关阅读:
    微软ASP.NET网站部署指南(4):配置项目属性
    iOS 设计模式之抽象工厂
    How can I move a MySQL database from one server to another?
    CentOS 7 上安装vim(默认未安装)
    How to resize slide dimensions without resizing any objects on the slide?
    CentOS 7.3 上安装docker
    美国留学访学(访问学者)必备信用卡
    西安理工大学税务登记证、银行账号信息
    Oracle联合多个子查询(inner join)
    linux tail
  • 原文地址:https://www.cnblogs.com/lgx5/p/13196180.html
Copyright © 2011-2022 走看看