我想计算一个滑动时间范围内的事件数量.
例如,假设我想知道Google股票(GOOG)在过去1000秒内有多少出价.
我正在尝试以下查询:
SELECT
symbol,
start_date,
start_time,
bid_price,
count(if(max(start_time)-start_time<1000,1,null)) over (partition by symbol order by start_time asc) cnt
FROM [bigquery-samples:nasdaq_stock_quotes.quotes]
where symbol = 'GOOG'
Run Code Online (Sandbox Code Playgroud)
逻辑如下:分区窗口(按符号)与投标时间一起订购(为简单起见,单独留下投标日期).对于每个窗口(由窗口"head"处的行定义),我想计算start_time小于1000秒的行数,而不是"head"行时间.
我正在尝试使用max(start_time)来获取窗口中的顶行.这似乎不起作用,我收到一个错误:
Error: MAX is an analytic function and must be accompanied by an OVER clause.
Run Code Online (Sandbox Code Playgroud)
是否可以在一列中包含两个分析函数(在这种情况下都是count和max)?是否有不同的解决方案来解决问题?
小智 8
尝试使用范围功能.
SELECT
symbol,
start_date,
start_time,
bid_price,
count(market_center) over (partition by symbol order by start_time RANGE 1000 PRECEDING) cnt
FROM [bigquery-samples:nasdaq_stock_quotes.quotes]
where symbol = 'GOOG'
order by 2, 3
Run Code Online (Sandbox Code Playgroud)
我只使用market_center作为计数器,也可以使用其他字段.
注意:BigQuery查询参考中没有记录RANGE函数,但它是一个标准的SQL函数,在这种情况下似乎可以工作