【发布时间】: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