[英]How do I return the date range of a max rolling average value?
I have the following query : 我有以下查询:
SELECT account,
FLOOR(max(mov_avg_7d)) AS max_mov_avg_7d
FROM (
SELECT account,date,items,
AVG(items) OVER (PARTITION BY account ORDER BY date RANGE BETWEEN 6 PRECEDING AND CURRENT ROW) AS mov_avg_7d,
FROM [my_table]
)
group by account
Here is a sample of my table : 这是我桌子的样品:
Account Date Items
accountxxxxxx 2009-01-01 235
accountxxxxxx 2009-01-02 261
accountxxxxxx 2009-01-03 186
accountxxxxxx 2009-01-04 173
accountxxxxxx 2009-01-05 273
accountxxxxxx 2009-01-06 254
accountxxxxxx 2009-01-07 386
With FLOOR(max(mov_avg_7d)) AS max_mov_avg_7d
I'm able to retrieve the highest average number of Items an account can have over a 7 day rolling period. 使用FLOOR(max(mov_avg_7d)) AS max_mov_avg_7d
我可以检索一个帐户在7天的滚动期内可以拥有的平均最高物品数。
I would like to be able to have for each account the date range (7 days) associated with the highest average number of Items over 7 days. 我希望每个帐户都可以拥有与7天内平均商品数最高相关的日期范围(7天)。
Output would be something like this : 输出将是这样的:
Account Date Items max_mov_avg_7d min_date_range max_date_range
accountxxxxxx 2009-01-01 235 635 2009-05-12 2009-05-19
Hopefully, I'm clear enough. 希望我已经足够清楚了。
Thanks ! 谢谢 !
Simon. 西蒙
#standardSQL
SELECT
account,
ARRAY_AGG(STRUCT(date, items, mov_avg_7d) ORDER BY mov_avg_7d DESC LIMIT 1)[OFFSET(0)].*
FROM (
SELECT account,date,items,
FLOOR(AVG(items) OVER (PARTITION BY account ORDER BY date RANGE BETWEEN 6 PRECEDING AND CURRENT ROW)) AS mov_avg_7d
FROM `my_table`
)
group by account
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.