Redshift 无法插入,时间戳列是字符串类型

Redshift cannot insert, timestamp column is of type string

我在 redshift 中有两个 tables,它们是彼此的精确副本,(扫描,scans_staging),一个是暂存 [​​=30=],另一个是主要 table。他们有以下 DDL:

CREATE TABLE scans_staging (
    id text,
    imb text,
    scandatetime timestamp without time zone,
    scaneventcode text,
    status text,
    anticipateddeliverydate timestamp without time zone,
    scanfacilityname text,
    scanfacilitystate text,
    scanfacilitycity text,
    scanfacilityzip text
);

并且我正在尝试 运行 查询以将数据从暂存 table 更新到扫描 table,如下所示:

insert into scans
select scans_staging.*
from scans
right join scans_staging on scans.id = scans_staging.id
where scans.id is null

但是我收到错误:Invalid operation: column "scandatetime" is of type timestamp without time zone but expression is of type character varying

但是当我查看两个 table 中的数据时,时间戳的格式完全相同,例如 2020-11-23 16:17:02。他们在 yyyy-MM-dd HH:mm:ss。我在这里犯了什么菜鸟错误?

编辑:这是以下 table def 查询的结果:

select "column", type, encoding, distkey, sortkey, "notnull" 
from pg_table_def
where tablename = 'scans' 

两个 tables

Scans:

|column                 |type                       |encoding|distkey|sortkey  |notnull|
----------------------------------------------------------------------------------------
|id                     |character varying(256)     |lzo     |false  |0        |false  |
|imb                    |character varying(256)     |lzo     |false  |0        |false  |
|scandatetime           |timestamp without time zone|az64    |false  |0        |false  |
|scaneventcode          |character varying(256)     |lzo     |false  |0        |false  |
|status                 |character varying(256)     |lzo     |false  |0        |false  |
|anticipateddeliverydate|timestamp without time zone|az64    |false  |0        |false  |
|scanfacilityname       |character varying(256)     |lzo     |false  |0        |false  |
|scanfacilitystate      |character varying(256)     |lzo     |false  |0        |false  |
|scanfacilitycity       |character varying(256)     |lzo     |false  |0        |false  |
|scanfacilityzip        |character varying(256)     |lzo     |false  |0        |false  |

Scans_staging:

|column                 |type                       |encoding|distkey|sortkey  |notnull|
----------------------------------------------------------------------------------------
|id                     |character varying(256)     |lzo     |false  |0        |false  |
|imb                    |character varying(256)     |lzo     |false  |0        |false  |
|scandatetime           |timestamp without time zone|az64    |false  |0        |false  |
|scaneventcode          |character varying(256)     |lzo     |false  |0        |false  |
|status                 |character varying(256)     |lzo     |false  |0        |false  |
|anticipateddeliverydate|timestamp without time zone|az64    |false  |0        |false  |
|scanfacilityname       |character varying(256)     |lzo     |false  |0        |false  |
|scanfacilitystate      |character varying(256)     |lzo     |false  |0        |false  |
|scanfacilitycity       |character varying(256)     |lzo     |false  |0        |false  |
|scanfacilityzip        |character varying(256)     |lzo     |false  |0        |false  |

如果两个表的结构完全相同,则不会出现该错误。

我将从枚举 insertselect 子句中的列开始,以避免列未出现在两个表中相同位置的潜在问题:

insert into scans (
       id, imb, scandatetime, scaneventcode, status, anticipateddeliverydate, scanfacilityname, scanfacilitystate, scanfacilitycity, scanfacilityzip
)
select id, imb, scandatetime, scaneventcode, status, anticipateddeliverydate, scanfacilityname, scanfacilitystate, scanfacilitycity, scanfacilityzip
from scans_staging ss
where not exists  (select 1 from scans s where s.id = ss.id)

如果您仍然收到类型不匹配错误,这表明您的列具有不同的数据类型。如果是这样,您需要在 select 子句中设置额外的转换。