Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Vertica分析函数,用于计算窗口中的实例数_Sql_Vertica - Fatal编程技术网

Sql Vertica分析函数,用于计算窗口中的实例数

Sql Vertica分析函数,用于计算窗口中的实例数,sql,vertica,Sql,Vertica,假设我有一个包含两列的数据集:ID和timestamp。我的目标是统计在任何30天窗口中至少有n个时间戳的返回ID 以下是一个例子: ID Timestamp 1 '2019-01-01' 2 '2019-02-01' 3 '2019-03-01' 1 '2019-01-02' 1 '2019-01-04' 1 '2019-01-17' 假设我想返回一个ID列表,在任何30天的窗口中都有3个时间戳 如上所述,我的结果集应该是ID=1。我想某种窗口功能可以实现这一点,但我不确定 你有

假设我有一个包含两列的数据集:
ID
timestamp
。我的目标是统计在任何30天窗口中至少有n个时间戳的返回ID

以下是一个例子:

ID Timestamp
1  '2019-01-01'
2  '2019-02-01'
3  '2019-03-01'
1  '2019-01-02'
1  '2019-01-04'
1  '2019-01-17'
假设我想返回一个ID列表,在任何30天的窗口中都有3个时间戳

如上所述,我的结果集应该是ID=1。我想某种窗口功能可以实现这一点,但我不确定


你有没有可能帮我写一个查询来完成这个任务?

一个相对简单的方法是
lag()
/
lead()


啊,明白了,所以您实际上是在使用函数的lag(col,n)n部分来回过头来查找第三条记录。这很有道理。这太完美了!所以一般来说,如果你在寻找n个时间戳,它会滞后(col,n-1)。@ben890。
lag()
lead()
将起作用。是的,第二个参数是
n-1
。只需确保
datediff()
的参数顺序正确即可。或者使用
abs(datediff())
select t.*
from (select t.*,
             lead(timestamp, 2) over (partition by id order by timestamp) as timestamp_2
      from t
     ) t
where datediff(day, timestamp, timestamp_2) <= 30;
select distinct id
from (select t.*,
             lead(timestamp, 2) over (partition by id order by timestamp) as timestamp_2
      from t
     ) t
where datediff(day, timestamp, timestamp_2) <= 30;