SQL 插入失败 i.name 不存在,但在插入期间看似存在

SQL Insert fails on i.name does not exist, when it seemingly does, during insert

我正在使用 Postgres SQL 和 pgAdmin。我正在尝试使用 INSERT INTO 和 SELECT FROM 语句以及 to_char 语句在暂存 table 和生产 table 之间复制数据。这可能是也可能不是错误的方法。 SELECT 失败,因为显然是 "column i.dates does not exist"。

问题是:为什么我得到 'column i.dates does not exist'

两个 table 的架构除了日期转换外是相同的。

我尝试匹配 table 的模式,但 to_char 转换除外。我已经检查并仔细检查了该列是否存在。

这是我正在尝试的代码:

INSERT INTO weathergrids (location, dates, temperature, rh, wd, ws, df, cu, cc)
  SELECT
    i.location AS location,
    i.dates as dates,
    i.temperature as temperature,
    i.rh as rh,
    i.winddir as winddir,
    i.windspeed as windspeed, 
    i.droughtfactor as droughtfactor,
    i.curing as curing,
    i.cloudcover as cloudcover
  FROM (
      SELECT location, 
             to_char(to_timestamp(dates, 'YYYY-DD-MM HH24:MI'), 'HH24:MI YYYY-MM-DD HH24:MI'), 
             temperature, rh, wd, ws, df, cu, cc 
       FROM wosweathergrids
  ) i;

我收到的错误是:

ERROR:  column i.dates does not exist
LINE 4:  i.dates as dates,
         ^
SQL state: 42703
Character: 151

我的数据模式是这样的:


+-----------------+-----+-------------+-----------------------------+-----+
|      TABLE      | NUM |   COLNAME   |          DATATYPE           | LEN |
+-----------------+-----+-------------+-----------------------------+-----+
| weathergrids    |   1 | id          | integer                     |  32 |
| weathergrids    |   2 | location    | numeric                     |   6 |
| weathergrids    |   3 | dates       | timestamp without time zone |     |
| weathergrids    |   4 | temperature | numeric                     |   3 |
| weathergrids    |   5 | rh          | numeric                     |   4 |
| weathergrids    |   6 | wd          | numeric                     |   4 |
| weathergrids    |   7 | wsd         | numeric                     |   4 |
| weathergrids    |   8 | df          | numeric                     |   4 |
| weathergrids    |   9 | cu          | numeric                     |   4 |
| weathergrids    |  10 | cc          | numeric                     |   4 |
| wosweathergrids |   1 | id          | integer                     |  32 |
| wosweathergrids |   2 | location    | numeric                     |   6 |
| wosweathergrids |   3 | dates       | character varying           |  16 |
| wosweathergrids |   4 | temperature | numeric                     |   3 |
| wosweathergrids |   5 | rh          | numeric                     |   4 |
| wosweathergrids |   6 | wd          | numeric                     |   4 |
| wosweathergrids |   7 | ws          | numeric                     |   4 |
| wosweathergrids |   8 | df          | numeric                     |   4 |
| wosweathergrids |   9 | cu          | numeric                     |   4 |
| wosweathergrids |  10 | cc          | numeric                     |   4 |
+-----------------+-----+-------------+-----------------------------+-----+

您的名为 i 的派生 table(子查询)没有名为 dates 的列,因为 dates 列在 to_char() 函数,并且由于它没有为该表达式定义别名,因此派生的 table.

的列 dates 不可用 "outside"

但我看不出派生 table 的原因。另外:也不需要为同名的列设置别名 i.location as locationi.location 完全相同。

因此您的查询可以简化为:

INSERT INTO weathergrids (location, dates, temperature, rh, wd, ws, df, cu, cc)
SELECT
    location,
    to_timestamp(dates, 'YYYY-DD-MM HH24:MI'),
    temperature,
    rh,
    winddir,
    windspeed, 
    droughtfactor,
    curing,
    cloudcover
FROM wosweathergrids

您不需要为 to_timestamp() 表达式指定别名,因为列是按位置匹配的,而不是在 insert ... select 语句中按名称匹配。