将现有表持续到月末,并提供预测数据并每天更新

问题描述 投票:0回答:1

我想在Google Big Query中使用现有的每日收入数据创建一个新表,并使用基于现有数据并需要创建的预测数据来扩展此新表。一旦存在某天的新实际数据,它将覆盖该天的预测数据。同样,直到月底的预测数据也会再次更新。

到目前为止,我提出了以下内容,该内容生成了错误消息Scalar subquery produced more than one element

        SELECT
    date, sum(yl_revenue), 'ACTUAL' as type 
    from project.dataset.table 
    where date >"2020-01-01" and date < current_date() 
    group by date 
    union distinct

    SELECT 
    (select calendar_date 
FROM UNNEST(GENERATE_DATE_ARRAY('2020-01-01', DATE_SUB(DATE_TRUNC(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), MONTH), INTERVAL 1 DAY), INTERVAL 1 DAY)) AS calendar_date) AS calendar_date, 
    avg(revenue_daily) as average_daily_revenue, 
    'FORECAST' as type FROM 
        (SELECT sum(revenue) as revenue_daily from project.dataset.table 
WHERE date > "2020-01-01" and extract(month from date) = extract (month from current_date()) group by date) 

我希望数据看起来如何:

+------------+------------+----------+
|    date    |  revenue   |   type   |
+------------+------------+----------+
| 01.04.2020 | 100 €      | ACTUAL   |
| …          | 5.000 €    | ACTUAL   |
| 23.04.2020 | 200 €      | ACTUAL   |
| 24.04.2020 |  230,43 €  | FORECAST |
| 25.04.2020 |  230,43 €  | FORECAST |
| 26.04.2020 |  230,43 €  | FORECAST |
| 27.04.2020 |  230,43 €  | FORECAST |
| 28.04.2020 |  230,43 €  | FORECAST |
| 29.04.2020 |  230,43 €  | FORECAST |
| 30.04.2020 |  230,43 €  | FORECAST |
+------------+------------+----------+

[第二天(2020年4月24日)应该看起来像这样:

+------------+--------------+----------+
|    date    |   revenue    |   type   |
+------------+--------------+----------+
| 01.04.2020 | 100 €        | ACTUAL   |
| …          | 5.000 €      | ACTUAL   |
| 23.04.2020 | 200 €        | ACTUAL   |
| 24.04.2020 |  1.000,00 €  | ACTUAL   | <----
| 25.04.2020 |  262,50 €    | FORECAST |
| 26.04.2020 |  262,50 €    | FORECAST |
| 27.04.2020 |  262,50 €    | FORECAST |
| 28.04.2020 |  262,50 €    | FORECAST |
| 29.04.2020 |  262,50 €    | FORECAST |
| 30.04.2020 |  262,50 €    | FORECAST |
+------------+--------------+----------+

[预测值只是该月的实际收入之和除以该月到目前为止的天数。请注意,第二个表中的每日预测值已更改,因为新的实际值已添加到其中。

非常感谢您提供有关如何解决此问题的帮助!

谢谢

Jan

sql google-bigquery forecasting
1个回答
1
投票

更新新的一天-您可以在下面运行以更新其余的日子

UPDATE `project.dataset.table`
SET revenue = (
  SELECT ROUND(SUM(revenue) / COUNT(1), 2) 
  FROM `project.dataset.table`
  WHERE type = 'ACTUAL'
)
WHERE type = 'FORECAST'   

以上假设您具有每月表格,其中预先创建了所有日期如果您有其他布局-可以很容易地对其进行调整

© www.soinside.com 2019 - 2024. All rights reserved.