【问题标题】:Join Alias Columns SQL加入别名列 SQL
【发布时间】:2017-04-02 18:30:26
【问题描述】:

我正在努力加入别名命名列。总的来说,我想要一个带有日期、小时以及实际和预测(前一天上午 10 点之前的最新)风速的输出。

我得到以下代码:

错误:“日期”列不存在
第 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)

【问题讨论】:

  • 为什么是 MySQL 标签?
  • 错误已修复。 @草莓
  • ... date_trunc('day', meso.timestmp)::date = Foredate AND ...?除了grouporder 子句之外,不可能在任何地方使用列的别名。

标签: postgresql join


【解决方案1】:

从那里看不到“日期”别名。

你可以在 WITH 之后使用几个表,所以我建议你将第二个选择移到那里。

我不完全确定 weather.meso 表结构,但根据您的查询进行猜测,这应该可行:

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

【讨论】:

  • 它不太管用,但有助于我的理解。我收到一条错误消息,提示缺少 tmp 中表“forecast_prep”的 FROM 子句。添加之后就是说 tmp 中的所有选择都需要在 GROUP BY 子句中。
  • 你是对的。我错过了这个,但用最新的编辑修复了它;)
  • 另外,因为我不知道您表中的所有列名,所以我对 SELECT 进行了一些更改,以避免出现模棱两可的列命名问题。
  • 仍然是错误的,因为avgtmp查询中没有group by...
  • 不客气。如果查询最终有效,我们将不胜感激。
猜你喜欢
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-06
相关资源
最近更新 更多