TDengine icon indicating copy to clipboard operation
TDengine copied to clipboard

聚合查询interval,聚合时间点不正确

Open Luokun2016 opened this issue 5 years ago • 6 comments

背景:使用taosdemo 创建的测试数据库 @localvar @hjxilinx 使用interval聚合时,聚合时间点不正确

taos> select avg(f1) from test.t1 where ts >='2017-07-13 16:00:00:000' and ts <'2017-07-20 16:00:00:000' interval(1d) fill(NUll);
           ts            |          avg(f1)          |
======================================================
 2017-07-13 00:00:00.000 |                      NULL |
 2017-07-14 00:00:00.000 |               4.505052189 |
 2017-07-15 00:00:00.000 |               4.503009558 |
 2017-07-16 00:00:00.000 |                      NULL |
 2017-07-17 00:00:00.000 |                      NULL |
 2017-07-18 00:00:00.000 |                      NULL |
 2017-07-19 00:00:00.000 |                      NULL |
 2017-07-20 00:00:00.000 |                      NULL |
Query OK, 8 row(s) in set (0.003869s)

使用**interval(1d)**是一天开始时间,以上结果可以理解

taos> select avg(f1) from test.t1 where ts >='2017-07-13 16:00:00:000' and ts <'2017-07-20 16:00:00:000' interval(24h) fill(NUll);
           ts            |          avg(f1)          |
======================================================
 2017-07-13 08:00:00.000 |                      NULL |
 2017-07-14 08:00:00.000 |               4.511451972 |
 2017-07-15 08:00:00.000 |               4.479289686 |
 2017-07-16 08:00:00.000 |                      NULL |
 2017-07-17 08:00:00.000 |                      NULL |
 2017-07-18 08:00:00.000 |                      NULL |
 2017-07-19 08:00:00.000 |                      NULL |
 2017-07-20 08:00:00.000 |                      NULL |
Query OK, 8 row(s) in set (0.003860s)

使用interval(24h),为什么聚合结果从2017-07-13 08:00:00.000开始,而不是**2017-07-13 16:00:00:000'**开始?

Luokun2016 avatar Dec 30 '20 03:12 Luokun2016

taos> select * from test.t1 where ts >='2020-12-27T16:00:00.000Z' and ts <'2021-01-03T15:59:59.999Z';
           ts            |     f1      |     f2      |     f3      |
====================================================================
 2020-12-30 05:00:00.000 |           1 |           2 |           3 |
 2020-12-30 05:00:01.000 |           1 |           2 |           3 |
Query OK, 2 row(s) in set (0.002224s)

聚合查询,为什么是从2020-12-27 08:00:00.000开始?(因为服务器时区是UTC+8,导致从8:00开始?)导致2020-12-30**的2条数据,聚合到12-29中。

taos> select sum(f1) from test.t1 where ts >='2020-12-27T16:00:00.000Z' and ts <'2021-01-03T15:59:59.999Z' interval(24h) fill(null);
           ts            |        sum(f1)        |
==================================================
 2020-12-27 08:00:00.000 |                  NULL |
 2020-12-28 08:00:00.000 |                  NULL |
 2020-12-29 08:00:00.000 |                     2 |
 2020-12-30 08:00:00.000 |                  NULL |
 2020-12-31 08:00:00.000 |                  NULL |
 2021-01-01 08:00:00.000 |                  NULL |
 2021-01-02 08:00:00.000 |                  NULL |
 2021-01-03 08:00:00.000 |                  NULL |
Query OK, 8 row(s) in set (0.002180s)

Luokun2016 avatar Dec 30 '20 05:12 Luokun2016

h 的语义就是从最近的时间整点开始计算。如果需要可以用 1d 查询到想要的结果为什么一定要用 24h 呢?

sangshuduo avatar Dec 30 '20 06:12 sangshuduo

h 的语义就是从最近的时间整点开始计算。如果需要可以用 1d 查询到想要的结果为什么一定要用 24h 呢?

表中数据的时间戳为UTC时间,使用1d查询的是UTC对应天的聚合数据,我需要UTC+8时区对应天的聚合数据。

Luokun2016 avatar Dec 30 '20 06:12 Luokun2016

或者把用时区设置把时间戳调整到+8时区的时间,或者用 INTERVAL (interval [, offset]) 来调整

sangshuduo avatar Dec 30 '20 09:12 sangshuduo

或者把用时区设置把时间戳调整到+8时区的时间,或者用 INTERVAL (interval [, offset]) 来调整

$"interval(1d, {(offsetHours > 0 ? (24 - offsetHours) : Math.Abs(offsetHours))}h)";

Luokun2016 avatar Dec 30 '20 09:12 Luokun2016

请问有遇到interval(1n)出现的同样的问题吗,这怎么偏移呢

twinkleeyes avatar Sep 13 '23 06:09 twinkleeyes