根据年,月,季度提取数据

时间:2020-05-12 04:02:39

标签: mysql google-bigquery

我有一个输入和输出表,如下所示。输入表每天更新。我需要找到上一个财政年度,上个月,上一季度的值。我在下面编写了查询,以查找上一个财政年度的值。

在ReportFinancialYear ='FY19'的情况下,则Value else null结束为previous_financial_year_value

但是我想使用ReportFinancialYear =当前财政年度1,类似这样。其余类似公式。谁能帮忙。

enter image description here

1 个答案:

答案 0 :(得分:1)

对于BigQuery标准SQL

有点长,但是我测试并完成了工作。

上一个财政年度:

(CASE
    WHEN CAST(REPLACE(UPPER(ReportFinancialYear), "FY", "") AS INT64) + 2000 = EXTRACT(YEAR FROM CURRENT_DATE) - 1 THEN Value
    ELSE NULL
END) AS previous_financial_year_value

上一个财政季度:

(CASE
    WHEN ((CAST(REPLACE(UPPER(ReportFinancialYear), "FY", "") AS INT64) + 2000 = EXTRACT(YEAR FROM CURRENT_DATE) - 1) AND (CAST(REPLACE(UPPER(ReportFinancialQuarter), "Q", "") AS INT64) = 4) AND (EXTRACT(QUARTER FROM CURRENT_DATE) = 1)) OR (CAST(REPLACE(UPPER(ReportFinancialYear), "FY", "") AS INT64) + 2000 = EXTRACT(YEAR FROM CURRENT_DATE) AND (CAST(REPLACE(UPPER(ReportFinancialQuarter), "Q", "") AS INT64) = EXTRACT(QUARTER FROM CURRENT_DATE) - 1)) THEN Value
    ELSE NULL
END) AS previous_financial_quarter_value

上个月:

(CASE
    WHEN ReportMonth = DATE_SUB(DATE_TRUNC(CURRENT_DATE, MONTH), INTERVAL 1 MONTH) THEN Value
    ELSE NULL
END) AS previous_financial_month_value

编辑

由于您还要求提供当前季度

(CASE
    WHEN (CAST(REPLACE(UPPER(ReportFinancialYear), "FY", "") AS INT64) + 2000 = EXTRACT(YEAR FROM CURRENT_DATE)) AND (CAST(REPLACE(UPPER(ReportFinancialQuarter), "Q", "") AS INT64) = EXTRACT(QUARTER FROM CURRENT_DATE)) THEN Value
    ELSE NULL
END) AS current_financial_quarter_value

注意::我从previous_financial_quarter_value中错误地复制了查询。现在,它已更新并更正。