Sql – Cannot cast type record to integer

postgresqlsql

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

Best Answer

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;
Related Topic