在 bigquery 中的最后提取日期计算未来 18 个月的预测
Posted
技术标签:
【中文标题】在 bigquery 中的最后提取日期计算未来 18 个月的预测【英文标题】:calculate forecast for next 18 months on last extract date in bigquery 【发布时间】:2018-01-19 11:35:13 【问题描述】:我想根据最后提取日期(每个星期一)检查未来 18 个月的预测数量。如果最后提取日期是 2018 年 3 月,则应从 2018 年 3 月开始计算未来 18 个月。
我在 bigquery 中尝试了以下案例语句,但它应该将上周一日期作为提取日期(extract_date),并根据材料名称(材料)和交货日期(交付日期)提供接下来 6、12、18 个月的数量总和
SELECT
material,
material_desc,
extract_date,
deliv_date,
SUM(quantity) AS quantity,
CASE
WHEN
(MONTH(deliv_date)=1 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=2 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=3 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=4 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=5 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=6 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=7 AND year(deliv_date)=2018) THEN '6months'
WHEN
(MONTH(deliv_date)=1 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=2 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=3 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=4 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=5 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=6 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=7 AND year(deliv_date)=2018 ) OR
(MONTH(deliv_date)=8 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=9 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=10 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=11 AND year(deliv_date)=2018) OR
(MONTH(deliv_date)=12 AND year(deliv_date)=2018) THEN '12Months'
and same code for 18 months...
ELSE NULL END AS Forecast_18_months
FROM table
GROUP BY
material,
material_desc,
extract_date,
deliv_date,
Forecast_18_months
【问题讨论】:
【参考方案1】:这是一个粗略的想法。请参阅 Bigquery SQL 指南并对其进行修改以适合您的用例。
SELECT
material
forcast
SUM(quantity)
FROM
(
SELECT
material
CASE
WHEN DATE_DIFF(deliv_date, extract_date, MONTH) <= 6 THEN '6months'
ELSE 'blabla'
END AS forecast
quantity
FROM
table
)
GROUP BY
material
forecast
【讨论】:
我们如何计算 18 个月的日期差异? 只需添加更多 WHEN 语句即可检查长达 18 个月的不同案例。以上是关于在 bigquery 中的最后提取日期计算未来 18 个月的预测的主要内容,如果未能解决你的问题,请参考以下文章
在 bigquery 标准 sql 上提取两个日期之间的小时数