如何插入非分组数据

时间:2016-11-16 16:13:33

标签: sql postgresql group-by

受此great answer的启发,我编写了以下查询,返回按去年5分钟间隔计算的AVG。

我想要的是所有5分钟的间隔,如果没有行适合特定的时间跨度,则设置为null

with intervals as (select
                     (select min("timestamp") from public.hst_energy_d) + n AS start_timestamp,
                     (select min("timestamp") from public.hst_energy_d) + n + 299 AS end_timestamp
                   from generate_series(extract(epoch from now())::BIGINT - 10596096000, extract(epoch from now())::BIGINT, 300) n)
(SELECT AVG(meas."Al1") as "avg", islots.start_timestamp AS "timestamp"
FROM public.hst_energy_d meas
  RIGHT OUTER JOIN intervals islots
    on meas.timestamp >= islots.start_timestamp and meas.timestamp <= islots.end_timestamp
WHERE
  meas.idinstrum = 4
  AND
  meas.id_device = 122
  AND
  meas.timestamp > extract(epoch from now()) - 10596096000
GROUP BY islots.start_timestamp, islots.end_timestamp
ORDER BY timestamp);

2 个答案:

答案 0 :(得分:1)

我想我知道你正在尝试做什么,我想知道如果使用interval '5 minutes'大概是一种更好,更容易遵循的方法:

with times as (  -- find the first date in the dataset, up to today
  select
    date_trunc ('minutes', min("timestamp")) - 
    mod (extract ('minutes' from min("timestamp"))::int, 5) * interval '1 minute' as bt,
    date_trunc ('minutes', current_timestamp) - 
    mod (extract ('minutes' from current_timestamp)::int, 5) * interval '1 minute' as et
  from hst_energy_d
  where
    idinstrum = 4 and
    id_device = 122
), -- generate every possible range between these dates
ranges as (
  select
    generate_series(bt, et, interval '5 minutes') as range_start
  from times
), -- normalize your data to which 5-minut interval it belongs to
rounded_hst as (
  select
    date_trunc ('minutes', "timestamp") - 
    mod (extract ('minutes' from "timestamp")::int, 5) * interval '1 minute' as round_time,
    *
  from hst_energy_d
  where
    idinstrum = 4 and
    id_device = 122  
)
select
  r.range_start, r.range_start + interval '5 minutes' as range_end,
  avg (hd."Al1")
from
  ranges r
  left join rounded_hst hd on
    r.range_start = hd.round_time
group by
  r.range_start
order by
  r.range_start

顺便说一句,挑剔的眼睛可能想知道为什么要烦恼CTE rounded_hst,为什么不在连接中使用“between”。从我测试和观察到的所有内容中,数据库将爆炸所有可能性,然后测试条件之间的等于where子句 - 过滤的笛卡尔。对于这么多的间隔,这肯定是一个杀手。

将每个数据截断到最接近的五分钟允许标准SQL连接。我鼓励你测试两者,我想你会明白我的意思。

- 编辑2016年11月17日 -

考虑时间的OP解决方案是数字,而不是日期:

with times as (  -- find the first date in the dataset, up to today
    select
      date_trunc('minutes', to_timestamp(min("timestamp"))::timestamp) -
      mod(extract ('minutes' from to_timestamp(min("timestamp"))::timestamp)::int, 5) * interval '1 minute' as bt,
      date_trunc('minutes', current_timestamp::timestamp) -
      mod(extract ('minutes' from (current_timestamp)::timestamp)::int, 5) * interval '1 minute' as et
    from hst_energy_d
    where
      idinstrum = 4 and
      id_device = 122
), -- generate every possible range between these dates
    ranges as (
      select
        generate_series(bt, et, interval '5 minutes') as range_start
      from times
  ), -- normalize your data to which 5-minute interval it belongs to
    rounded_hst as (
      select
        date_trunc ('minutes', to_timestamp("timestamp")::timestamp)::timestamp -
        mod (extract ('minutes' from (to_timestamp("timestamp")::timestamp))::int, 5) * interval '1 minute' as round_time,
        *
      from hst_energy_d
      where
        idinstrum = 4 and
        id_device = 122
  )
select
  extract('epoch' from r.range_start)::bigint, extract('epoch' from r.range_start + interval '5 minutes')::bigint as range_end,
  avg (hd."Al1")
from
  ranges r
  left join rounded_hst hd on
                             r.range_start = hd.round_time
group by
  r.range_start
order by
  r.range_start;

答案 1 :(得分:0)

我认为这篇文章适合你 Group DateTime into 5,15,30 and 60 minute intervals

这是一种分组日期的方法,我建议建立一个标量函数。