You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQLite窗口函数计算时窗口大小不足如何返回NULL结果

SQLite 滑动窗口行数不足时返回NULL的实现方法

你原有语句的窗口规则rows 19 preceding默认会在分区内数据不足20行时,基于现有可用行数计算指标,要实现不足20行返回NULL的效果,只需通过count(*)统计当前窗口内的总行数,增加条件判断即可。

修改后的完整SQL如下:

create table boll as select
    *,
    case when count(*) over win = 20 
         then (avg(close) over win)-2*sqrt((avg(close*close) over win)-pow((avg(close) over win),2)) 
         else NULL end as BOLD,
    case when count(*) over win = 20 
         then (avg(close) over win)+2*sqrt((avg(close*close) over win)-pow((avg(close) over win),2)) 
         else NULL end as BOLU
from bhav
window win as (partition by isin order by timestamp rows 19 preceding);

逻辑说明

  • count(*) over win会返回当前滑动窗口内的总数据行数,你定义的窗口满额状态为「当前行+前19行」共20行,所以当统计值等于20时才执行布林带计算
  • 不满足行数条件时直接返回NULL,符合需求

内容的提问来源于stack exchange,提问作者pii_ke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 08:45:02