【问题标题】:How to convert date to fiscal year and fiscal quarter when my fiscal year does not start in Jan?当我的会计年度不是从一月开始时,如何将日期转换为会计年度和会计季度?
【发布时间】:2022-08-03 13:45:45
【问题描述】:

我有一个日期字段,例如 2022-04-19,并且希望能够将其转换为财政年度字段 2022-Q3,因为财政年度从 8 月开始。如何编写大查询?谢谢!

    标签: google-bigquery


    【解决方案1】:

    您可以将日期向前移动 5 个月以获得从一月开始的季度周期。请注意,为了正确处理日期,我们将日期截断为月初。

    最后我们正确格式化

    WITH table AS (
    SELECT 
    DATE_ADD(DATE_TRUNC (DATE "2022-04-19", MONTH) ,INTERVAL 5 MONTH ) AS date
    )
    
    SELECT  FORMAT_DATE("%G- %Q", ) as format_date;
    

    【讨论】:

      【解决方案2】:

      你可以试试这个查询:

      SELECT date,
          concat(fiscal_year,'-Q',fiscal_month) as fiscal_quarter
      FROM
      (SELECT 
          date,
          extract(year from date_add(date, interval 5 month)) as fiscal_year,
          extract(quarter from date_add(date, interval 5 month)) as fiscal_month
      FROM `your-table`)
      ORDER BY date;
      

      输出:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-10
        • 2016-02-25
        • 1970-01-01
        • 1970-01-01
        • 2022-11-01
        • 2016-06-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多