SQLite Window 函数:当 window 大小不可用时给出 NULL 结果
SQLite Window functions: give NULL result when sufficient window size is not available
create table boll as select
*, (avg(close) over win)-2*sqrt((avg(close*close) over win)-pow((avg(close) over win),2)) as BOLD,
(avg(close) over win)+2*sqrt((avg(close*close) over win)-pow((avg(close) over win),2)) as BOLU
from bhav
window win as (partition by isin order by timestamp rows 19 preceding);
对于上述查询,如果当前行之前的行少于 19 行,是否可以通过 window win
的计算来 return NULL
?
只需使用 LAG
查看 19 个时间戳:
create table boll as
select *,
case when lag(timestamp, 19) over (order by timestamp) is not null then
avg(close) over win) - 2 * sqrt((avg(close*close) over win) - pow((avg(close) over win), 2)
end as BOLD,
case when lag(timestamp, 19) over (order by timestamp) is not null then
avg(close) over win) + 2 * sqrt((avg(close*close) over win) - pow((avg(close) over win), 2)
end as BOLU
from bhav
window win as (partition by isin order by timestamp rows 19 preceding);
您可以在您定义的 window 上使用 COUNT()
window 函数来检查有多少行:
create table boll as
select *,
CASE WHEN COUNT(*) OVER win >= 19 THEN (avg(close) over win) - 2 * sqrt((avg(close*close) over win)-pow((avg(close) over win),2)) END as BOLD,
CASE WHEN COUNT(*) OVER win >= 19 THEN (avg(close) over win)+2*sqrt((avg(close*close) over win)-pow((avg(close) over win),2)) END as BOLU
from bhav
window win as (partition by isin order by timestamp rows 19 preceding);
create table boll as select
*, (avg(close) over win)-2*sqrt((avg(close*close) over win)-pow((avg(close) over win),2)) as BOLD,
(avg(close) over win)+2*sqrt((avg(close*close) over win)-pow((avg(close) over win),2)) as BOLU
from bhav
window win as (partition by isin order by timestamp rows 19 preceding);
对于上述查询,如果当前行之前的行少于 19 行,是否可以通过 window win
的计算来 return NULL
?
只需使用 LAG
查看 19 个时间戳:
create table boll as
select *,
case when lag(timestamp, 19) over (order by timestamp) is not null then
avg(close) over win) - 2 * sqrt((avg(close*close) over win) - pow((avg(close) over win), 2)
end as BOLD,
case when lag(timestamp, 19) over (order by timestamp) is not null then
avg(close) over win) + 2 * sqrt((avg(close*close) over win) - pow((avg(close) over win), 2)
end as BOLU
from bhav
window win as (partition by isin order by timestamp rows 19 preceding);
您可以在您定义的 window 上使用 COUNT()
window 函数来检查有多少行:
create table boll as
select *,
CASE WHEN COUNT(*) OVER win >= 19 THEN (avg(close) over win) - 2 * sqrt((avg(close*close) over win)-pow((avg(close) over win),2)) END as BOLD,
CASE WHEN COUNT(*) OVER win >= 19 THEN (avg(close) over win)+2*sqrt((avg(close*close) over win)-pow((avg(close) over win),2)) END as BOLU
from bhav
window win as (partition by isin order by timestamp rows 19 preceding);