我想查看按小時和天分解的歷史實際和預測風。PostgreSQL查詢:在截止日期前獲取最新預測,與實際比較
我對一天中的某個小時有多個預測。而且我的交易截止日期爲美國東部時間上午10點,因此我希望在此之前的最新預測與該小時的實際風險相同。
複雜的事情是,時間戳是格林威治標準時間,這是比美國東部時間早5小時。
WITH
forecast_prep AS (
SELECT
date_trunc('day', (foretime - interval '5 hours')) :: DATE AS Foredate,
extract(HOUR FROM (foretime - interval '5 hours')) + 1 AS foreHE,
lat,
lon,
max(windspeed) as forecast,
max(as_of) - interval '5 hours' AS as_of
FROM weather.forecast
WHERE date_trunc('day', foretime) :: DATE - as_of >= INTERVAL '9 hours'
GROUP BY Foredate, foreHE, lat, lon
),
tmp AS (
SELECT
meso.station,
meso.lat,
meso.lon,
(meso.timestmp - interval '5 hours') as timestmp,
date_trunc('day', (meso.timestmp - interval '5 hours')) :: DATE AS Date,
extract(HOUR FROM (meso.timestmp - interval '5 hours')) + 1 AS HE,
CAST(AVG(meso.windspd) AS NUMERIC(19, 2)) AS Actual
FROM weather.meso
GROUP BY station, lat, lon, timestmp, Date, HE
)
SELECT
tmp.station, tmp.Date, tmp.HE, tmp.Actual, forecast_prep.forecast, forecast_prep.as_of
FROM tmp
INNER JOIN forecast_prep ON (
tmp.lat = forecast_prep.lat
AND tmp.lon = forecast_prep.lon
AND tmp.Date = forecast_prep.Foredate
AND tmp.HE = forecast_prep.foreHE
)
WHERE
(tmp.timestmp BETWEEN '2016-02-01' AND '2016-02-02')
AND (tmp.station = 'KSBN')
GROUP BY
tmp.station, tmp.Date, tmp.HE, forecast_prep.forecast, forecast_prep.as_of, tmp.Actual
ORDER BY tmp.Date, tmp.HE ASC;
下面是具有相關示例數據的完整表結構。
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 17:00', 34.556, 28.345),
(10.39, '2/1/2016 8:00', '1/31/2016 23:00', 34.556, 28.345),
(9.22, '2/1/2016 8:00', '2/1/2016 5:00', 34.556, 28.345),
(10, '2/1/2016 9:00', '1/30/2016 11:00', 34.556, 28.345),
(9.88, '2/1/2016 9:00', '1/30/2016 17: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', 34.556, 28.345),
(10.35, '2/1/2016 9:00', '1/31/2016 11:00', 34.556, 28.345),
(10.07, '2/1/2016 9:00', '1/31/2016 17:00', 34.556, 28.345),
(9.57, '2/1/2016 9:00', '1/31/2016 23:00', 34.556, 28.345),
(7.93, '2/1/2016 9:00', '2/1/2016 5:00', 34.556, 28.345)
;
CREATE TABLE weather.meso
(
timestmp timestamp without time zone NOT NULL,
station text NOT NULL,
lat numeric NOT NULL,
lon numeric NOT NULL,
tmp numeric,
hum numeric,
windspd numeric,
winddir integer,
dew numeric,
CONSTRAINT meso_pkey PRIMARY KEY (timestmp, station, lat, lon)
);
INSERT INTO weather.meso
(station, timestmp, lat, lon, windspd)
VALUES
('KSBN', '2/1/2016 8:02', 34.556, 28.345, 16.1),
('KSBN', '2/1/2016 8:12', 34.556, 28.345, 12.6),
('KSBN', '2/1/2016 8:54', 34.556, 28.345, 11.5),
('KSBN', '2/1/2016 9:02', 34.556, 28.345, 18.1),
('KSBN', '2/1/2016 9:17', 34.556, 28.345, 12.2),
('KSBN', '2/1/2016 9:48', 34.556, 28.345, 11.5)
;
這是我所希望輸出的格式:
station date he actual forecast as_of
KSBN 2/1/2016 4 10.4 15.1 1/31/2016 6:00
KSBN 2/1/2016 5 12.7 11.32 1/31/2016 6:00
提供一些源數據 - 以可重用的格式 - 以及預期的結果 - 是獲取可行解決方案的最快方法。 –
@Used_By_Already很抱歉提出一個新問題,但提供某些源數據的最佳方式是什麼? – otterdog2000
** **最好是每個表和一組插入的DDL,或者就像一個簡單的文本表(像你的sql代碼一樣呈現)是好的,或者作爲附加的文本文件或電子表格。 9我不喜歡電子表格,因爲它們會產生混亂以進行清理。)請記住,我們不想處理大量表格 - 它只是一個需要的樣本。添加到你的問題,所以每個人都可以找到它。請參閱https://stackoverflow.com/help/mcve –