根据米哈伊尔的回答(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 |
+-------+---------+----------+-----------+-----------+
答案 0 :(得分:5)
以下是BigQuery Standard SQL的示例
#standardSQL
SELECT *,
IFNULL(SUM(sales_total)
OVER(rolling_last_week), 0) last_week
FROM `project.dataset.your_table`
WINDOW rolling_last_week AS (
PARTITION BY id
ORDER BY DATE_DIFF(PARSE_DATE('%m-%d-%y', sales_date), '2000-01-01', WEEK)
RANGE BETWEEN 1 PRECEDING AND 1 PRECEDING
)
ORDER BY id, sales_date
您可以使用问题中的虚拟数据进行测试/播放,如下所示
#standardSQL
WITH `project.dataset.your_table` AS (
SELECT 1 id, '01-01-17' sales_date, 1 weeknumber, 5 sales_total UNION ALL
SELECT 1, '01-02-17', 1, 5 UNION ALL
SELECT 1, '01-03-17', 1, 5 UNION ALL
SELECT 1, '01-04-17', 1, 5 UNION ALL
SELECT 1, '01-05-17', 1, 5 UNION ALL
SELECT 1, '01-06-17', 1, 5 UNION ALL
SELECT 1, '01-07-17', 1, 5 UNION ALL
SELECT 1, '01-08-17', 2, 10 UNION ALL
SELECT 1, '01-09-17', 2, 10 UNION ALL
SELECT 1, '01-10-17', 2, 10 UNION ALL
SELECT 1, '01-11-17', 2, 10 UNION ALL
SELECT 1, '01-12-17', 2, 10 UNION ALL
SELECT 1, '01-13-17', 2, 10 UNION ALL
SELECT 1, '01-14-17', 2, 10
)
SELECT *,
IFNULL(SUM(sales_total)
OVER(rolling_last_week), 0) last_week
FROM `project.dataset.your_table`
WINDOW rolling_last_week AS (
PARTITION BY id
ORDER BY DATE_DIFF(PARSE_DATE('%m-%d-%y', sales_date), '2000-01-01', WEEK)
RANGE BETWEEN 1 PRECEDING AND 1 PRECEDING
)
ORDER BY id, sales_date
结果
Row id sales_date weeknumber sales_total last_week
1 1 01-01-17 1 5 0
2 1 01-02-17 1 5 0
3 1 01-03-17 1 5 0
4 1 01-04-17 1 5 0
5 1 01-05-17 1 5 0
6 1 01-06-17 1 5 0
7 1 01-07-17 1 5 0
8 1 01-08-17 2 10 35
9 1 01-09-17 2 10 35
10 1 01-10-17 2 10 35
11 1 01-11-17 2 10 35
12 1 01-12-17 2 10 35
13 1 01-13-17 2 10 35
14 1 01-14-17 2 10 35