1

我对 Postgres 很陌生,需要获取两个单独的数据:1)表 T1 的各种记录/结果的平均值、最小值、最大值 2)表 T1 的“最后”记录的列值基于最最近的时间戳

问题是我不能单独运行这些查询,因为它会导致性能问题。(这个表中的数据可以是几万条记录,也可以是记录,组合成一个结果对象,甚至更复杂)。

是否可以将这两个数据的结果并排组合成一个查询怪物,该查询将返回所需的输出?

感谢你的帮助。

更新了查询:

第一个查询:

select
    rtp.id, rtp.received_timestamp,
    rtp.agent_time, rtp.sourceip, rtp.destip, rtp.sourcedscp,
    sum(rtp.numduplicate) as dups, avg(rtp.numduplicate) as avgdups,
    min(rtp.numduplicate) as mindups, max(rtp.numduplicate) as maxdups
from rtp_test_result rtp
where
    rtp.received_timestamp between 1274723208 and 1475642299
group by rtp.sourceip, rtp.destip, rtp.sourcedscp
order by rtp.sourceip, rtp.destip, rtp.sourcedscp

第二个查询:

select id, received_timestamp, numooo
from rtp_test_result 
where received_timestamp = (select max(received_timestamp) mrt from rtp_test_result)
group by id,received_timestamp, numooo 
order by id desc limit 1
4

1 回答 1

2

就像是

with cte as (
    select
        val,
        last_value(val) over(order by ts asc rows between unbounded preceding and unbounded following) as lst_value
    from T1
)
select
    avg(val) as avg_value,
    min(val) as min_value,
    max(val) as max_value,
    max(lst_value) as lst_value
from cte

或者

select
    avg(val) as avg_value,
    min(val) as min_value,
    max(val) as max_value,
    (select val from T1 order by ts desc limit 1) as lst_value
from T1

sql fiddle demo

于 2013-08-22T10:20:16.530 回答