我有許多天氣位置和風預報數據。我需要在前一天的10:00
前最近的as_of
。我需要每個小時,每一天,每個位置。獲取帶有多列組標識符的最新預測數據
一個位置被定義爲一個唯一的lat
和lon
對。與相關的樣本數據
全表模式:
CREATE SCHEMA weather
CREATE TABLE weather.forecast
(
foretime timestamp without time zone NOT NULL,
as_of timestamp without time zone NOT NULL, -- in UTC
summary text,
precipintensity numeric(8,4),
precipprob numeric(2,2),
temperature numeric(5,2),
apptemp numeric(5,2),
dewpoint numeric(5,2),
humidity numeric(2,2),
windspeed numeric(5,2),
windbearing numeric(4,1),
visibility numeric(5,2),
cloudcover numeric(4,2),
pressure numeric(6,2),
ozone numeric(5,2),
preciptype text,
lat numeric(8,6) NOT NULL,
lon numeric(9,6) NOT NULL,
CONSTRAINT forecast_pkey PRIMARY KEY (foretime, as_of, lat, lon)
);
INSERT INTO weather.forecast
(windspeed, foretime, as_of, lat, lon)
VALUES
(11.19, '2/1/2016 8:00', '1/30/2016 23:00', 34.556, 28.345),
(10.98, '2/1/2016 8:00', '1/31/2016 5:00', 34.556, 28.345),
(10.64, '2/1/2016 8:00', '1/31/2016 11:00', 34.556, 28.345),
(10.95, '2/1/2016 8:00', '1/31/2016 8:00', 29.114, 16.277),
(10.39, '2/1/2016 8:00', '1/31/2016 23:00', 29.114, 16.277),
(9.22, '2/1/2016 8:00', '1/31/2016 5:00', 29.114, 16.277),
(10, '2/1/2016 9:00', '1/30/2016 04:00', 34.556, 28.345),
(9.88, '2/1/2016 9:00', '1/31/2016 09:00', 34.556, 28.345),
(10.79, '2/1/2016 9:00', '1/30/2016 23:00', 34.556, 28.345),
(10.8, '2/1/2016 9:00', '1/31/2016 5:00', 29.114, 16.277),
(10.35, '2/1/2016 9:00', '1/31/2016 11:00', 29.114, 16.277),
(10.07, '2/1/2016 9:00', '1/31/2016 17:00', 29.114, 16.277)
;
期望的結果格式:
lat lon Foredate foreHE windspeed as_of
34.556 28.345 2/1/2016 8 10.98 1/31/2016 5:00
34.556 28.345 2/1/2016 9 9.88 1/31/2016 9:00
29.114 16.277 2/1/2016 8 10.95 1/31/2016 8:00
29.114 16.277 2/1/2016 9 10.80 1/31/2016 5:00
這裏是我的代碼以獲得正確的as_of
。當我嘗試重新加入風速時,事情就變糟了。
SELECT
date_trunc('day', (a.foretime)) :: DATE AS Foredate,
extract(HOUR FROM (a.foretime)) AS foreHE,
a.lat,
a.lon,
max(a.as_of) - interval '5 hours' as latest_as_of
FROM weather.forecast a
WHERE date_trunc('day', foretime) :: DATE - as_of >= INTERVAL '14 hours'
GROUP BY Foredate, foreHE, a.lat, a.lon
我不知道你是怎麼處理我的最新預測的約束在前一天上午10點的之前。其中一些最近的時間已經過了上午10點。這是我的查詢代碼中的WHERE條款的目的。 – otterdog2000
@ otterdog2000我已經根據您的要求更改了它 –
謝謝,當我將它與我的完整代碼結合使用時,我無法停下來。查詢只是運行直到我殺了它。 – otterdog2000