【问题标题】:PGSQL syntax to fetch last year's last two month records from current year Jan or Feb用于从当年一月或二月获取去年最后两个月记录的 PGSQL 语法
【发布时间】:2016-08-18 14:30:15
【问题描述】:

谁能告诉我从明年 1 月或 2 月获取去年最后 2 个月记录的逻辑?例如:我想比较 2016 年 1 月作为本月的销售额,将 2015 年 12 月作为上个月的销售额,将 2015 年 11 月作为两个月前的销售额。我试过这样,但如果是一月或二月就不行了

(case when extract(month from m.validfrom) = extract(month from current_date)-1 then 'Previous Month'
when extract(month from m.validfrom) = extract(month from current_date)-2 then 'Two Months Before'
when extract(month from m.validfrom) = extract(month from current_date) then 'Current Month' end ) as month,

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    date_trunc

    case date_trunc('month', m.validfrom)
        when date_trunc('month', current_date - interval '1 month') then 'Previous Month'
        when date_trunc('month', current_date - interval '2 month') then 'Two Months Before'
        when date_trunc('month', current_date) then 'Current Month'
    end as month
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      相关资源
      最近更新 更多