Good afternoon! I am trying to take values that span multiple columns in one table and insert them into a single column in a new table, but for multiple different fields into a few different fields in the new table.
E.g. in my original table there are 5 columns for different medications, I am wanting to insert them into one column in the new table and just have the other column values duplicate in rows for each medication that was inserted.
The error message I get is that there are more expressions than targets, which yes there are, but some of the inserts are labeled the same. Does 'Insert' not allow for inserting multiple things into one column? Is there another method or function to use for this?
Thank you in advance!
This is my newly created table:
CREATE TABLE source.roster_new
(
person_number text COLLATE pg_catalog."default",
date_of_birth timestamp without time zone,
person_last_name text COLLATE pg_catalog."default",
person_first_name text COLLATE pg_catalog."default",
dx_code text COLLATE pg_catalog."default",
dx_description text COLLATE pg_catalog."default",
dx_first_date timestamp without time zone,
gender text COLLATE pg_catalog."default",
age numeric,
last_test_type text COLLATE pg_catalog."default",
last_test_date timestamp without time zone,
medication text COLLATE pg_catalog."default"
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
And this is my code for inserting into that new table from the old one:
INSERT INTO source.roster_new
SELECT
person_number
,date_of_birth
,person_last_name
,person_first_name
,asthma_code AS dx_code
,copd_code AS dx_code
,asthma_description AS dx_description
,copd_description AS dx_description
,first_asthma_date AS dx_first_date
,first_copd_date AS dx_first_date
,gender
,age
,case when(last_flu_date IS NOT NULL) THEN 'Flu' End AS last_test_type
,case when(last_spiro_date IS NOT NULL) THEN 'Spirometry' End AS last_test_type
,case when(last_pneumo_date IS NOT NULL) THEN 'Pneumococcal' End AS last_test_type
,last_flu_date AS last_test_date
,last_spiro_date AS last_test_date
,last_pneumo_date AS last_test_date
,medication1 AS medication
,medication2 AS medication
,medication3 AS medication
,medication4 AS medication
,medication5 AS medication
FROM source.roster
EDIT: I couldn't figure out how to format tables, so I uploaded two images of the results I'm hoping to see and what the original data would look like.