簡體   English   中英

如何返回最大滾動平均值的日期范圍?

[英]How do I return the date range of a max rolling average value?

我有以下查詢:

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

這是我桌子的樣品:

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

使用FLOOR(max(mov_avg_7d)) AS max_mov_avg_7d我可以檢索一個帳戶在7天的滾動期內可以擁有的平均最高物品數。

我希望每個帳戶都可以擁有與7天內平均商品數最高相關的日期范圍(7天)。

輸出將是這樣的:

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

希望我已經足夠清楚了。

謝謝 !

西蒙

#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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM