计算bigquery中最后一个提取日期的未来18个月的预测

时间:2018-01-19 11:35:14

标签: google-bigquery

我想根据最后提取日期(每个星期一)查看未来18个月的预测数量。如果最后提取日期是2018年3月,则应该计算从2018年3月开始的未来18个月。

我在bigquery中尝试了以下案例陈述,但它应该将最后一个星期日作为提取日期(extract_date),并根据物料名称(物料)和交货日期(deliv_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 个答案:

答案 0 :(得分: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