05_查每一天的first_value及last_value值,并计算标准差

该博客展示了如何在MatrixDB中查询每一天的温度传感器数据的第一个和最后一个值,并计算这些值的标准差。通过创建插件、建表、插入数据,然后使用窗口函数获取每天的first_value和last_value,最后计算标准差,提供了完整的SQL查询示例。
摘要由CSDN通过智能技术生成

Matrixdb - 05查每一天的first_value及last_value值,并计算标准差

作者

shidb

日期

2021-05-13

标签

05查每一天的first_value及last_value值,并计算标准差

创建插件

create extension matrixts ;

建表及插入数据

create table t_sensor(
ts timestamp,
temp float);

insert into t_sensor select t,random()*100 from generate_series('2021-01-01'::date,'2021-12-31'::date,'1 day') as t ;
insert into t_sensor select t,random()*100 from generate_series('2021-01-01'::date,'2021-12-31'::date,'1 day') as t ;
insert into t_sensor select t,random()*100 from generate_series('2021-01-01'::date,'2021-12-31'::date,'1 day') as t ;

查看数据样例

select * 
from t_sensor 
where ts>='2021-01-01' and ts<='2021-01-05' 
order by 1 asc;

         ts          |        temp        
---------------------+--------------------
 2021-01-01 00:00:00 |   71.8015190388499
 2021-01-01 00:00:00 | 63.486212374267126
 2021-01-01 00:00:00 |  70.91826008314719
 2021-01-02 00:00:00 |   71.4020209487007
 2021-01-02 00:00:00 |   59.2465096839927
 2021-01-02 00:00:00 | 57.372795700993606

取每一天得第一个值和最后一个值

select a.ts,
first_value(a.temp) over(partition by a.ts order by a.ts asc),
last_value(a.temp) over(partition by a.ts order by a.ts asc rows between unbounded preceding and unbounded following)
from (
select time_bucket(interval '1 day',ts) as ts,temp
from t_sensor
where ts>='2021-01-01' and ts<='2021-01-05' 
order by 1 asc) a ;
         ts          |    first_value     |     last_value     
---------------------+--------------------+--------------------
 2021-01-01 00:00:00 |   71.8015190388499 |  70.91826008314719
 2021-01-01 00:00:00 |   71.8015190388499 |  70.91826008314719
 2021-01-01 00:00:00 |   71.8015190388499 |  70.91826008314719
 2021-01-02 00:00:00 |   71.4020209487007 | 57.372795700993606
 2021-01-02 00:00:00 |   71.4020209487007 | 57.372795700993606
 2021-01-02 00:00:00 |   71.4020209487007 | 57.372795700993606
 2021-01-03 00:00:00 | 1.1092143910147456 |  50.22444328867657
 2021-01-03 00:00:00 | 1.1092143910147456 |  50.22444328867657
 2021-01-03 00:00:00 | 1.1092143910147456 |  50.22444328867657
 2021-01-04 00:00:00 | 34.349012488451436 |  8.795154478600153
 2021-01-04 00:00:00 | 34.349012488451436 |  8.795154478600153
 2021-01-04 00:00:00 | 34.349012488451436 |  8.795154478600153
 2021-01-05 00:00:00 | 40.136026895945065 | 18.006777436564647
 2021-01-05 00:00:00 | 40.136026895945065 | 18.006777436564647
 2021-01-05 00:00:00 | 40.136026895945065 | 18.006777436564647
(15 rows)

取每一天得第一条

select c.ts,c.f_temp,c.l_temp
from (select row_number() over(partition by b.ts) as rn,b.ts,b.f_temp,b.l_temp
from (select a.ts,
first_value(a.temp) over(partition by a.ts order by a.ts asc) as f_temp,
last_value(a.temp) over(partition by a.ts order by a.ts asc rows between unbounded preceding and unbounded following) as l_temp
from (
select time_bucket(interval '1 day',ts) as ts,temp
from t_sensor
where ts>='2021-01-01' and ts<='2021-01-05' 
order by 1 asc) a ) b ) c 
where c.rn=1;
         ts          |       f_temp       |       l_temp       
---------------------+--------------------+--------------------
 2021-01-01 00:00:00 |   71.8015190388499 |  70.91826008314719
 2021-01-02 00:00:00 |   71.4020209487007 | 57.372795700993606
 2021-01-03 00:00:00 | 1.1092143910147456 |  50.22444328867657
 2021-01-04 00:00:00 | 34.349012488451436 |  8.795154478600153
 2021-01-05 00:00:00 | 40.136026895945065 | 18.006777436564647
(5 rows)

计算标准差

select stddev(c.f_temp),stddev(c.l_temp)
from (select row_number() over(partition by b.ts) as rn,b.ts,b.f_temp,b.l_temp
from (select a.ts,
first_value(a.temp) over(partition by a.ts order by a.ts asc) as f_temp,
last_value(a.temp) over(partition by a.ts order by a.ts asc rows between unbounded preceding and unbounded following) as l_temp
from (
select time_bucket(interval '1 day',ts) as ts,temp
from t_sensor
where ts>='2021-01-01' and ts<='2021-01-05' 
order by 1 asc) a ) b ) c 
where c.rn=1;
       stddev       |      stddev       
--------------------+-------------------
 29.458434663208738 | 26.52395750014408
(1 row)

视频版

https://member.bilibili.com/platform/upload-manager/article

如需入群沟通交流,请扫码添加好友
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值