Sql BigQuery:在多行中重复相同的计算值

Sql BigQuery:在多行中重复相同的计算值,sql,google-bigquery,Sql,Google Bigquery,我正在尝试使用Googe Big Query将几个简单的查询放入一个新表中。在最后一个表中,是我可以从另一个表中简单地提取的现有每日收入数据。然后,我想计算当月每天的平均收入,并将此值持续到月末。因此,最终表格每天更新,包括实际数据和预测数据 到目前为止,我提出了以下内容,它们组合生成了一条错误消息:标量子查询生成了多个元素 预测值只是该月的实际收入除以该月迄今为止的天数之和 感谢您提供有关如何处理此问题的任何提示。我刚刚想出了一些办法,创建了我需要的数据。我仍然会每天自动更新这个。但到目前为止

我正在尝试使用Googe Big Query将几个简单的查询放入一个新表中。在最后一个表中,是我可以从另一个表中简单地提取的现有每日收入数据。然后,我想计算当月每天的平均收入,并将此值持续到月末。因此,最终表格每天更新,包括实际数据和预测数据

到目前为止,我提出了以下内容,它们组合生成了一条错误消息:标量子查询生成了多个元素

预测值只是该月的实际收入除以该月迄今为止的天数之和


感谢您提供有关如何处理此问题的任何提示。

我刚刚想出了一些办法,创建了我需要的数据。我仍然会每天自动更新这个。但到目前为止,我得到的是:

select 
date, 'actual' as type, sum(revenue) as revenue from `project.dataset.revenue` where date >="2020-01-01" and date < current_date() group by date
union distinct
select calendar_date, 'forecast',(SELECT  avg(revenue_daily)  FROM 
(select  sum(revenue) as revenue_daily from `project.dataset.revenue` WHERE extract(year from date) = extract (year from current_date()) and extract(month from date) = extract (month from current_date()) group by date order by date) as average_daily_revenue),  FROM `project.dataset.calendar` where calendar_date >= current_date() and calendar_date <=DATE_SUB(DATE_TRUNC(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), MONTH), INTERVAL 1 DAY) order 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 |
+------------+------------+----------+
select 
date, 'actual' as type, sum(revenue) as revenue from `project.dataset.revenue` where date >="2020-01-01" and date < current_date() group by date
union distinct
select calendar_date, 'forecast',(SELECT  avg(revenue_daily)  FROM 
(select  sum(revenue) as revenue_daily from `project.dataset.revenue` WHERE extract(year from date) = extract (year from current_date()) and extract(month from date) = extract (month from current_date()) group by date order by date) as average_daily_revenue),  FROM `project.dataset.calendar` where calendar_date >= current_date() and calendar_date <=DATE_SUB(DATE_TRUNC(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), MONTH), INTERVAL 1 DAY) order by date