【问题标题】:How could I calculate Quarter and Year to date (current month)我如何计算迄今为止的季度和年度(当前月份)
【发布时间】:2021-06-16 11:41:15
【问题描述】:

我有一个查询来计算每个区域的当前期间收入和总计,如下所示:

with TOTAL as (
    select
        "Period",
        "Zone",
        "Country",
        "Tag",
        "Name",
        "Program",
        "Revenue",
        DATE_TRUNC('QUARTER', "Period") "Quarter",
        DATE_TRUNC('YEAR', "Period")    "Year"
    from "Database"."Schema"."Revenue"
)
select
    total.*,
    IFF(UPPER("Zone") = 'EMEA', "Revenue", 0) as "Revenue EMEA",
    IFF(UPPER("Zone") = 'APAC', "Revenue", 0) as "Revenue APAC",
    IFF(UPPER("Zone") = 'NA', "Revenue", 0) as "Revenue NA",
    DATEADD(MONTH, -1, "Period") as "Period M1",
    LAG("Revenue", 1, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Period") as "Revenue M1",
    LAG("Revenue EMEA", 1, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Period") as "Revenue EMEA M1",
    LAG("Revenue APAC", 1, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Period") as "Revenue APAC M1",
    LAG("Revenue NA", 1, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Period") as "Revenue NA M1",
    DATEADD(MONTH, -2, "Period") as "Period M2",
    LAG("Revenue", 2, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Period") as "Revenue M2",
    LAG("Revenue EMEA", 2, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Period") as "Revenue EMEA M2",
    LAG("Revenue APAC", 2, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Period") as "Revenue APAC M2",
    LAG("Revenue NA", 2, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Period") as "Revenue NA M2"
from total

我怎样才能有效地计算相同的值(收入、EMEA、APAC、NA):

  • 季度至今(期间):介于“季度”(实际季度期间开始)和“期间”(实际报告期间)之间的值)
  • 年初至今(期间):介于“年份”(实际年份期间的开始)和“期间”(实际报告期间)之间的值)
  • 上一个完整季度(整个季度期间:自上一季度起 3 个月)
  • 去年全年(全年期间:去年所有月份)

编辑:

我已按季度划分。 现在我需要它,到目前为止我该怎么做?

SUM("Revenue") OVER (PARTITION BY "Zone","Country","Tag","Quarter","Year" "Name", "Program" ORDER BY "Period") as "Revenue Q",
SUM("Revenue EMEA") OVER (PARTITION BY "Zone","Country","Tag","Quarter","Year", "Name", "Program" ORDER BY "Period") as "Revenue EMEA Q",
SUM("Revenue APAC") OVER (PARTITION BY "Zone","Country","Tag","Quarter","Year", "Name", "Program" ORDER BY "Period") as "Revenue APAC Q",
SUM("Revenue NA") OVER (PARTITION BY "Zone","Country","Tag","Quarter","Year", "Name", "Program" ORDER BY "Period") as "Revenue NA Q"

【问题讨论】:

    标签: sql time snowflake-cloud-data-platform windowing


    【解决方案1】:

    听起来有点像“家庭作业”问题,但我想你会添加这样的 last year 问题。您应该能够根据此模式找出其余部分。没有一些样本数据很难回答:

    DATEADD(YEAR, -1, "Year") as "Year 1 ago",
    LAG("Revenue", 1, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Year") as "Revenue Y1 ago",
    LAG("Revenue EMEA", 1, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Year") as "Revenue EMEA Y1 ago",
    LAG("Revenue APAC", 1, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Year") as "Revenue APAC Y1 ago",
    LAG("Revenue NA", 1, 0) OVER (PARTITION BY "Zone","Country","Tag", "Name", "Program" ORDER BY "Year") as "Revenue NA Y1 ago"
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 2019-03-08
      • 2017-05-25
      • 2016-12-31
      • 2022-01-16
      • 2011-01-26
      • 1970-01-01
      相关资源
      最近更新 更多