繁体   English   中英

oracle sql到PostgreSQL级查询

[英]oracle sql to postgresql level query

我有一个Oracle Sql查询,其中提供了一个财政年度的月份列表。 我想转换为postgresql,因为它没有级别

select case
         when to_char(add_months(sysdate , -1), 'MM') >= 4 then
          to_char(add_months(sysdate , -1), 'YYYY') || '-' ||
          to_char(to_number(to_char(add_months(sysdate , -1), 'YYYY')) + 1)
         else
          to_char(to_number(to_char(add_months(sysdate , -1), 'YYYY')) - 1) || '-' ||
          to_char(add_months(sysdate , -1), 'YYYY')
       end FY,
       to_char(level, '00') MNTH
  from dual
connect by level <=12
select 
    case when date_part('month',current_date) >= 4 then
              concat(date_part('year', current_date), '-', date_part('year',current_date)+1)
         else
              concat(date_part('year', current_date)-1, '-', date_part('year',current_date))
    end FY
    , lpad(date_part('month',d)::varchar,2,'0') MNTH
from (select DATE '2008-01-01' + (interval '1' month * generate_series(0,11)) d ) y
  • current_date (而不是sysdate)
  • + Interval 'n Month' (而不是add_months)
  • generate series (而不是通过连接)
  • date_part('month',...以获取月份号(而不是to_char(...,'MM'))
  • concat()用于串联(也负责类型转换)

样本结果:

    FY  MNTH
1   2017-2018    01
2   2017-2018    02
3   2017-2018    03
4   2017-2018    04
5   2017-2018    05
6   2017-2018    06
7   2017-2018    07
8   2017-2018    08
9   2017-2018    09
10  2017-2018    10
11  2017-2018    11
12  2017-2018    12

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM