我对Postgres很新,需要获取两个独立的数据: 1)表T1的各种记录/结果的avg,min,max的值 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
答案 0 :(得分:1)
类似
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