【发布时间】:2018-04-19 14:53:17
【问题描述】:
基于米哈伊尔 (How to calculate rolling average of same weekday as current row in Big Query?) 的回答
我希望复制这一点,但改为使用周数。以下是我的查询。似乎因为一次 7 行的周数相同,所以它不能按预期工作
SELECT id,
date,
weeknumber,
sales_total,
SUM(net_sales_total) OVER(rolling_last_week) last_week
FROM (
SELECT id,
date,
sales_total,
EXTRACT(WEEK FROM entry_date) weeknumber
FROM `mydataset.mytable`
)
WINDOW rolling_last_week AS (
PARTITION BY id, weeknumber
ORDER BY entry_date
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING
)
ORDER BY weeknumber, entry_date
表_A
+-------+---------+----------+-----------+
| id | date |weeknumber|sales_total|
+-------+---------+----------+-----------+
| 1 | 01-01-17| 1 | 5 |
| 1 | 01-02-17| 1 | 5 |
| 1 | 01-03-17| 1 | 5 |
| 1 | 01-04-17| 1 | 5 |
| 1 | 01-05-17| 1 | 5 |
| 1 | 01-06-17| 1 | 5 |
| 1 | 01-07-17| 1 | 5 |
| 1 | 01-08-17| 2 | 10 |
| 1 | 01-09-17| 2 | 10 |
| 1 | 01-10-17| 2 | 10 |
| 1 | 01-11-17| 2 | 10 |
| 1 | 01-12-17| 2 | 10 |
| 1 | 01-13-17| 2 | 10 |
| 1 | 01-14-17| 2 | 10 |
+-------+---------+----------+-----------+
我希望结果查询返回上周每一天的销售额总和
+-------+---------+----------+-----------+-----------+
| id | date |weeknumber|sales_total| last_week |
+-------+---------+----------+-----------+-----------+
| 1 | 01-01-17| 1 | 5 | . |
| 1 | 01-02-17| 1 | 5 | . |
| 1 | 01-03-17| 1 | 5 | . |
| 1 | 01-04-17| 1 | 5 | . |
| 1 | 01-05-17| 1 | 5 | . |
| 1 | 01-06-17| 1 | 5 | . |
| 1 | 01-07-17| 1 | 5 | . |
| 1 | 01-08-17| 2 | 10 | 35 |
| 1 | 01-09-17| 2 | 10 | 35 |
| 1 | 01-10-17| 2 | 10 | 35 |
| 1 | 01-11-17| 2 | 10 | 35 |
| 1 | 01-12-17| 2 | 10 | 35 |
| 1 | 01-13-17| 2 | 10 | 35 |
| 1 | 01-14-17| 2 | 10 | 35 |
+-------+---------+----------+-----------+-----------+
【问题讨论】:
标签: sql google-bigquery