我在SQLite数据库中有一个时间序列,想对其进行分析。
时间序列的重要部分由具有不同但并非唯一的字符串值的列组成。我想做这样的事情:
Value concat countValue
A A 1
A A,A 1
B A,A,B 1
B A,B,B 2
B B,B,B 3
C B,B,C 1
B B,C,B 2
我不知道如何获取countValue列。它应该计算该分区的所有值等于当前行的值。
我试过了,但是它只计算分区中的所有值,而不是等于该行值的值。
SELECT
Value,
group_concat(Value) OVER wind AS concat,
Sum(Case When Value Like Value Then 1 Else 0 End) OVER wind AS countValue
FROM TimeSeries
WINDOW
wind AS (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
ORDER BY
date
;
该查询还受到以下因素的限制:
仅使用SQL是否有可能?
这是使用字符串函数的一种方法:
select
value,
group_concat(value) over wind as concat,
(
length(group_concat(value) over wind) - length(replace(group_concat(value) over wind, value, ''))
) / length(value) cnt_value
from timeseries
window wind as (order by date rows between 2 preceding and current row)
order by date;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句