Postgresql连续时间之间的平均间隔--按星期几

Postgresql连续时间之间的平均间隔--按星期几,postgresql,intervals,date-difference,Postgresql,Intervals,Date Difference,我试图得到时间戳变量的平均值和中位数/时间差/相对于前一个时间戳,得出数据是否延迟的预期值 也就是说,现在的时间戳和最近的时间戳之间的差异是否超过了当天时间戳和前一个时间戳之间的平均时间间隔?或多或少,我希望创建一个的lambda/mu项,具体来说,我希望这个平均值/中值对一周中的某一天敏感,就像每个道琼斯指数一样 我的桌子看起来像这样: 创建表事件 id序列非空主键, datetime timestamptz非空默认当前时间戳 ; 在事件中插入日期时间值 “2021-03-28 00:00:0

我试图得到时间戳变量的平均值和中位数/时间差/相对于前一个时间戳,得出数据是否延迟的预期值

也就是说,现在的时间戳和最近的时间戳之间的差异是否超过了当天时间戳和前一个时间戳之间的平均时间间隔?或多或少,我希望创建一个的lambda/mu项,具体来说,我希望这个平均值/中值对一周中的某一天敏感,就像每个道琼斯指数一样

我的桌子看起来像这样:

创建表事件 id序列非空主键, datetime timestamptz非空默认当前时间戳 ; 在事件中插入日期时间值 “2021-03-28 00:00:00-06”-说星期天是稀疏的,看每8小时一次,给或拿。。。。 '2021-03-28 08:00:00-06', '2021-03-28 16:00:00-06', '2021-03-28 23:59:00-06', '2021-04-04 01:00:00-06', '2021-04-04 09:00:00-06', '2021-04-04 16:30:00-06', “2021-03-29 00:05:00-06”-假设周一很密集,看,大约每小时一次 '2021-03-29 01:04:00-06', '2021-03-29 02:03:00-06', '2021-03-29 03:02:00-06', '2021-03-29 04:01:00-06', '2021-03-29 05:00:00-06', '2021-03-29 06:01:00-06', '2021-03-29 07:02:00-06', '2021-03-29 08:03:00-06', '2021-03-29 09:04:00-06', '2021-03-29 10:05:00-06', '2021-03-29 11:01:00-06', '2021-03-29 12:02:00-06', '2021-03-29 13:03:00-06', '2021-03-29 14:04:00-06', '2021-03-29 15:05:00-06', '2021-03-29 16:04:00-06', '2021-03-29 17:03:00-06', '2021-03-29 18:02:00-06', '2021-03-29 19:01:00-06', '2021-03-29 20:01:00-06', '2021-03-29 21:02:00-06', '2021-03-29 22:03:00-06', '2021-03-29 23:05:00-06', '2021-03-29 23:59:00-06', '2021-04-05 00:04:00-06', '2021-04-05 01:03:00-06', '2021-04-05 02:02:00-06', '2021-04-05 03:01:00-06', '2021-04-05 04:01:00-06', '2021-04-05 05:03:00-06', '2021-04-05 06:04:00-06', '2021-04-05 07:01:00-06', '2021-04-05 08:02:00-06', '2021-04-05 09:03:00-06', '2021-04-05 10:04:00-06', '2021-04-05 11:05:00-06', '2021-04-05 12:02:00-06', '2021-04-05 13:03:00-06', '2021-04-05 14:04:00-06', '2021-04-05 15:02:00-06', '2021-04-05 16:05:00-06', '2021-04-05 17:03:00-06', '2021-04-05 18:02:00-06', '2021-04-05 19:01:00-06', '2021-04-05 20:01:00-06', '2021-04-05 21:02:00-06', '2021-04-05 22:03:00-06', '2021-04-05 23:05:00-06', '2021-04-05 23:59:00-06' ; 我的初步询问如下:

子集为 从事件中选择* 其中,从datetime::timestamptz中提取道琼斯指数的时区“美国/芝加哥”=“0”-今天的子集是星期日 选择 LAGdatetime,按日期时间顺序为1,作为滞后时间间隔, 从AVGLAGdatetime中提取历元,按datetime的顺序1作为平均时间间隔,单位为秒, 从组LAGdatetime内的百分位0.5中提取历元,按日期时间顺序取1作为中位数间隔,单位为秒, 最新的maxdatetime 从子集开始; 你可以看到这个

我被难倒的地方是:

是将时间戳与前一个时间戳区分开来的正确操作符吗?这是一个rowid类型的函数?不同或更好的窗口功能?我现在得到的错误是:

查询错误:错误:滞后或接近滞后的语法错误

但我对此有点困惑,因为根据那个医生的说法,它看起来是正确的

如何处理分区中一天中的第一次和最后一次?如果我只是做了一个带有时区转换的星期日子集,我不希望本周星期日的开始追溯到上周星期日的结束,那太长了,我是否应该在我的外部FROM中添加where子句以排除除滞后之外的第一个观察结果? 使现代化 我认为这个查询更接近,它至少返回:

with subset as (
select * 
from Events
where extract (dow from datetime::timestamptz AT TIME ZONE 'America/Chicago')  = '0' -- subset on today is sunday
)
(select
  max(most_recent),
  AVG(ss.diff)/60 as avg_in_minutes,
  percentile_cont(0.5) 
    within group(order by ss.diff)/60 as q50_in_minutes,
  percentile_cont(0.05) 
    within group(order by ss.diff)/60 as q05_in_minutes,
  percentile_cont(0.95) 
    within group(order by ss.diff)/60 as q95_in_minutes
from (
  select 
  max(datetime) over (order by datetime) as most_recent,
  extract(epoch from datetime) as t1,
  extract(epoch from LAG(datetime,1) over (ORDER BY datetime)) as t0,
  extract(epoch from datetime) - extract(epoch from LAG(datetime,1) over (ORDER BY datetime)) as diff
  from subset
) as ss);
结果看起来不错

最大值 平均分钟数 q50分钟 q05分钟 q95分钟 2021-04-04T22:30:00.000Z 2214 480 456 7439.999999999998 试试这个:

模式PostgreSQL v13

问题1

类别 最大值 平均分钟数 q50分钟 q05分钟 q95分钟 A. 2021-04-04T22:30:00.000Z 1844.6666666666667 479 112.5 7005 B 2021-03-28T06:02:00.000Z 2. 2. 2. 2.
这是不正确的,因为两周之间有间隔。您需要具体按天进行聚合
create table Events (
  id serial NOT NULL PRIMARY KEY,
  datetime timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
  category text default 'A'
  );
  
 insert into Events (datetime, category) VALUES 
('2021-03-28 00:00:00-06', 'B'),
('2021-03-28 00:02:00-06', 'B');

  
insert into Events (datetime) VALUES 
('2021-03-28 00:00:00-06'),  -- say sunday is sparse, look it's every 8 hours-ISH, GIVE OR TAKE....
('2021-03-28 08:00:00-06'),
('2021-03-28 16:00:00-06'),
('2021-03-28 23:59:00-06'),

('2021-04-04 01:00:00-06'),
('2021-04-04 09:00:00-06'),
('2021-04-04 16:30:00-06'),

('2021-03-29 00:05:00-06'),  -- say monday is dense, look, it's hourly
('2021-03-29 01:04:00-06'),
('2021-03-29 02:03:00-06'),
('2021-03-29 03:02:00-06'),
('2021-03-29 04:01:00-06'),
('2021-03-29 05:00:00-06'),
('2021-03-29 06:01:00-06'),
('2021-03-29 07:02:00-06'),
('2021-03-29 08:03:00-06'),
('2021-03-29 09:04:00-06'),
('2021-03-29 10:05:00-06'),
('2021-03-29 11:01:00-06'),
('2021-03-29 12:02:00-06'),
('2021-03-29 13:03:00-06'),
('2021-03-29 14:04:00-06'),
('2021-03-29 15:05:00-06'),
('2021-03-29 16:04:00-06'),
('2021-03-29 17:03:00-06'),
('2021-03-29 18:02:00-06'),
('2021-03-29 19:01:00-06'),
('2021-03-29 20:01:00-06'),
('2021-03-29 21:02:00-06'),
('2021-03-29 22:03:00-06'),
('2021-03-29 23:05:00-06'),
('2021-03-29 23:59:00-06'),


('2021-04-05 00:04:00-06'),
('2021-04-05 01:03:00-06'),
('2021-04-05 02:02:00-06'),
('2021-04-05 03:01:00-06'),
('2021-04-05 04:01:00-06'),
('2021-04-05 05:03:00-06'),
('2021-04-05 06:04:00-06'),
('2021-04-05 07:01:00-06'),
('2021-04-05 08:02:00-06'),
('2021-04-05 09:03:00-06'),
('2021-04-05 10:04:00-06'),
('2021-04-05 11:05:00-06'),
('2021-04-05 12:02:00-06'),
('2021-04-05 13:03:00-06'),
('2021-04-05 14:04:00-06'),
('2021-04-05 15:02:00-06'),
('2021-04-05 16:05:00-06'),
('2021-04-05 17:03:00-06'),
('2021-04-05 18:02:00-06'),
('2021-04-05 19:01:00-06'),
('2021-04-05 20:01:00-06'),
('2021-04-05 21:02:00-06'),
('2021-04-05 22:03:00-06'),
('2021-04-05 23:05:00-06'),
('2021-04-05 23:59:00-06'
); 
with subset as (
select * 
from Events
where extract (dow from datetime::timestamptz AT TIME ZONE 'America/Chicago')  = '0' 
)
(select 
  category,
  max(most_recent),
  AVG(ss.diff)/60 as avg_in_minutes,
  percentile_cont(0.5) 
    within group(order by ss.diff)/60 as q50_in_minutes,
  percentile_cont(0.05) 
    within group(order by ss.diff)/60 as q05_in_minutes,
  percentile_cont(0.95) 
    within group(order by ss.diff)/60 as q95_in_minutes
from (
  select 
  category,
  max(datetime) over (partition by category order by datetime) as most_recent,
  extract(epoch from datetime) as t1,
  extract(epoch from LAG(datetime,1) over (ORDER BY datetime)) as t0,
  extract(epoch from datetime) - extract(epoch from LAG(datetime,1) over (ORDER BY datetime)) as diff
  from subset
) as ss
group by category);