简体   繁体   中英

Cannot cast type record to integer

I'm trying to insert data from one table to another by using:

INSERT INTO production.spend_fact (date, client_id, show_name, network_name,
media_type, spend, load_id, note)

SELECT date, client_id, show_name, network_name, media_type, spend, load_id,
note
FROM staging.spend_fact
JOIN
 (SELECT MAX(load_id)
  FROM production.load_dim) AS load_id
ON 1=1;

This query previously worked until I added a new column (note) to both tables and now I am getting this error:

ERROR: column "load_id" is of type integer but expression is of type record

I have tried casting "load_id" by CAST(load_id AS INT) and load_id::int but both do not work.

load_id FROM production.load_dim is type serial

load_id FROM production.performance_fact is type integer

You need columns names:

INSERT INTO production.spend_fact (date, client_id, show_name,
                                   network_name, media_type, spend, load_id, note)
    SELECT date, client_id, show_name, network_name, media_type, spend,
           l.load_id, note
    FROM staging.spend_fact CROSS JOIN
         (SELECT MAX(load_id) AS load_id
          FROM production.load_dim
         ) l;

you get this error because load_id is a table alias for your joined subquery

when you specify table name in select list postgres will select all columns of that table as a single row type value

you can directly acces row value field with following syntax: (load_id).max

or you can use different naming in your join:

join (select max(load_id) as load_id FROM production.load_dim) as dummy_name on true

or you can use subquery instead of join:

select date, client_id, show_name, network_name, media_type, spend,
(SELECT MAX(load_id) FROM production.load_dim) ,
note
FROM staging.spend_fact;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM