2016-11-18 45 views
3

我正在努力加入別名命名列。總的來說,我想要一個帶有日期,小時,以及實際和預測的輸出(最近一天早上10點前)風速。加入別名列SQL

有了下面的代碼我得到:

ERROR: column "date" does not exist
LINE xx: ...ast_prep.lat AND meso.lon = forecast_prep.lon AND Date ...

我無法弄清楚如何讓SQL加入這些命名的列。
謝謝。是的,我是一個SQL新手。

with forecast_prep as (
    SELECT 
    date_trunc('day', foretime)::date AS Foredate, 
    extract(hour from foretime)+1 AS foreHE, 
    lat, 
    lon, 
    windspeed, 
    max(as_of) AS as_of 
    FROM weather.forecast 
    WHERE date_trunc('day', foretime)::date-as_of>= interval '16 hours' 
    GROUP BY Foredate, foreHE, lat, lon, windspeed) 
SELECT 
    meso.station, 
    date_trunc('day', meso.timestmp)::date AS Date, 
    extract(hour from meso.timestmp)+1 AS HE, 
    CAST(AVG(meso.windspd) as numeric(19,2)) As Actual, 
    forecast_prep.windspeed, 
    forecast_prep.as_of 
FROM weather.meso 
    INNER JOIN forecast_prep ON (
    meso.lat = forecast_prep.lat AND 
    meso.lon = forecast_prep.lon AND 
    Date = Foredate AND ----<<<< Error here 
    HE = foreHE) 
WHERE 
    (meso.timestmp Between '2016-02-01' And '2016-02-02') AND 
    (meso.station='KSBN') 
GROUP BY meso.station, Date, HE, forecast_prep.windspeed, forecast_prep.as_of 
ORDER BY Date, HE ASC 

下面是表的結構:

-- Table: weather.forecast 

-- DROP TABLE weather.forecast; 

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) 


-- Table: weather.meso 

-- DROP TABLE weather.meso; 

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) 
+2

爲什麼MySQL的標籤? – Strawberry

+0

錯誤修復。 @Strawberry – otterdog2000

+1

'... date_trunc('day',meso.timestmp):: date = Foredate AND ...'?除了'group'和'order'子句外,不可能使用列的別名。 – Abelisto

回答

3

'日期' 別名不能從那裏看到。

您可以在WITH後使用很少的表格,所以我會建議您在此選擇第二個選擇。

我不是完全地肯定weather.meso表結構,而是由guesing根據您的查詢,這應該工作:

WITH 
    forecast_prep AS (
     SELECT 
       date_trunc('day', foretime) :: DATE AS Foredate, 
       extract(HOUR FROM foretime) + 1  AS foreHE, 
       lat, 
       lon, 
       max(windspeed) as windspeed, 
       max(as_of)       AS as_of 
     FROM weather.forecast 
     WHERE date_trunc('day', foretime) :: DATE - as_of >= INTERVAL '16 hours' 
     GROUP BY Foredate, foreHE, lat, lon 
    ), 
    tmp AS (
     SELECT 
     meso.station, 
     meso.lat, 
     meso.lon, 
     meso.timestmp, 
     date_trunc('day', meso.timestmp) :: DATE AS Date, 
     extract(HOUR FROM meso.timestmp) + 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.windspeed, 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.windspeed, forecast_prep.as_of, tmp.Actual 
ORDER BY tmp.Date, tmp.HE ASC; 

像第一個例子就在這裏https://www.postgresql.org/docs/8.4/static/queries-with.html

+1

它不太有用,但有助於我的理解。我得到一個錯誤,表示在tmp中缺少表「forecast_prep」的FROM子句。添加之後,說tmp中的所有這些選擇都需要位於GROUP BY子句中。 – otterdog2000

+0

你說得對。我錯過了這一點,但修復了最新的編輯;) –

+0

另外,因爲我不知道表中的所有列名,所以我改變了一點SELECT以避免模糊的列命名問題。 –