gpt4 book ai didi

postgresql - Postgres : getting the maximum and minimum values, 和它们出现的时间戳

转载 作者:行者123 更新时间:2023-11-29 11:59:03 24 4
gpt4 key购买 nike

我正在运行 Postgres 9.2,并且有一张温度和时间戳表,每分钟一个时间戳,以毫秒纪元时间表示:

weather=# \d weather_data
Table "public.weather_data"
Column | Type | Modifiers
-------------+--------------+-----------
timestamp | bigint | not null
sensor_id | integer | not null
temperature | numeric(4,1) |
humidity | integer |
date | date | not null
Indexes:
"weather_data_pkey" PRIMARY KEY, btree ("timestamp", sensor_id)
"weather_data_date_idx" btree (date)
"weather_data_humidity_idx" btree (humidity)
"weather_data_sensor_id_idx" btree (sensor_id)
"weather_data_temperature_idx" btree (temperature)
"weather_data_time_idx" btree ("timestamp")
Foreign-key constraints:
"weather_data_sensor_id_fkey" FOREIGN KEY (sensor_id) REFERENCES weather_sensors(sensor_id)

weather=# select * from weather_data order by timestamp desc;
timestamp | sensor_id | temperature | humidity | date
---------------+-----------+-------------+----------+------------
1483272420000 | 2 | 22.3 | 57 | 2017-01-01
1483272420000 | 1 | 24.9 | 53 | 2017-01-01
1483272360000 | 2 | 22.3 | 57 | 2017-01-01
1483272360000 | 1 | 24.9 | 58 | 2017-01-01
1483272300000 | 2 | 22.4 | 57 | 2017-01-01
1483272300000 | 1 | 24.9 | 57 | 2017-01-01
[...]

我有这个现有的查询,它获取每天的高点和低点,但不是高点或低点出现的特定时间:

WITH t AS (
SELECT date, highest, lowest
FROM (
SELECT date, max(temperature) AS highest
FROM weather_data
WHERE sensor_id = (SELECT sensor_id FROM weather_sensors WHERE sensor_name = 'outdoor')
GROUP BY date
ORDER BY date ASC
) h
INNER JOIN (
SELECT date, min(temperature) AS lowest
FROM weather_data
WHERE sensor_id = (SELECT sensor_id FROM weather_sensors WHERE sensor_name = 'outdoor')
GROUP BY date
ORDER BY date ASC
) l
USING (date)
ORDER BY date DESC
)
SELECT * from t ORDER BY date ASC;

数据库中有超过 200 万行,运行时间约为 1.2 秒,这还算不错。我现在想获得高点或低点的具体时间,我想出了这个使用窗口函数的方法,它确实有效但需要大约 5.6 秒:

SELECT h.date, high_time, high_temp, low_time, low_temp FROM (
SELECT date, high_temp, high_time FROM (
SELECT date, temperature AS high_temp, timestamp AS high_time, row_number()
OVER (PARTITION BY date ORDER BY temperature DESC, timestamp DESC)
FROM weather_data
WHERE sensor_id = (SELECT sensor_id FROM weather_sensors WHERE sensor_name = 'outdoor')
) highs
WHERE row_number = 1
) h
INNER JOIN (
SELECT * FROM (
SELECT date, temperature AS low_temp, timestamp AS low_time, row_number()
OVER (PARTITION BY date ORDER BY temperature ASC, timestamp DESC)
FROM weather_data
WHERE sensor_id = (SELECT sensor_id FROM weather_sensors WHERE sensor_name = 'outdoor')
) lows
WHERE row_number = 1
) l
ON h.date = l.date
ORDER BY h.date ASC;

我可以对第一个查询添加一些相对简单且不会增加大量执行时间的内容吗?我假设有,但我认为我已经关注这个问题太久了!

最佳答案

SELECT  
DISTINCT ON (zdate) zdate
, first_value(ztimestamp) OVER www AS stamp_at_min
, first_value(temperature) OVER www AS tmin
, last_value(ztimestamp) OVER www AS stamp_at_max
, last_value(temperature) OVER www AS tmax
FROM weather_data
WHERE sensor_id = 2
WINDOW www AS (PARTITION BY zdate ORDER BY temperature, ztimestamp
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
)
;

  • 前缀z日期和z时间戳
  • 我将 ztimestamp 添加到订单中作为决胜局

关于postgresql - Postgres : getting the maximum and minimum values, 和它们出现的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41415357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com