postgresql - SQL查询将多列插入一列,同时复制每一行中的其余字段
问题描述
下午好!我正在尝试将跨越一个表中的多个列的值插入到新表中的单个列中,但是对于多个不同的字段到新表中的几个不同字段中。
例如,在我的原始表中,有 5 列用于不同的药物,我想将它们插入到新表的一列中,而对于插入的每种药物,其他列值在行中重复。
我得到的错误消息是表达式多于目标,是的,但有些插入标记相同。“插入”是否不允许将多个内容插入一列?是否有其他方法或功能可用于此?
先感谢您!
这是我新创建的表:
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;
这是我从旧表插入新表的代码:
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
编辑:我不知道如何格式化表格,所以我上传了两张我希望看到的结果图像以及原始数据的样子。
解决方案
可以为您需要的单一选择使用联合
INSERT INTO source.roster_new (
person_number
,date_of_birth
,person_last_name
,person_first_name
,asthma_code AS dx_code
,dx_description
,dx_first_date
,gender
,age
,last_test_type
, last_test_date
,medication )
SELECT
person_number
,date_of_birth
,person_last_name
,person_first_name
,asthma_code AS dx_code
,asthma_description AS dx_description
,first_asthma_date AS dx_first_date
,gender
,age
,'Flu' End AS last_test_type
,last_flu_date AS last_test_date
,medication1 AS medication
FROM source.roster
where last_flu_date IS NOT NULL
UNION
SELECT
person_number
,date_of_birth
,person_last_name
,person_first_name
,asthma_code AS dx_code
,asthma_description AS
,first_asthma_date AS
,gender
,age
,'Spirometry'
,last_spiro_date
,medication2
FROM source.roster
where last_spiro_date IS NOT NULL
UNION
SELECT
person_number
,date_of_birth
,person_last_name
,person_first_name
,asthma_code AS dx_code
,asthma_description AS
,first_asthma_date AS
,gender
,age
,'last_pneumo_date'
,last_pneumo_date
,medication3
FROM source.roster
where last_pneumo_date
等等 ...
推荐阅读
- flutter - flutter riverpod:如何测试 asyncvalue.error?
- angular - 错误 TS2322:类型“列表 | undefined' 不可分配给类型 'any[] (Iterable
& 任何[]) | (任何[] & 可迭代 ) | 空 | 不明确的' - python - 获取单词的开始和结束索引?
- node.js - 如何使用 Apollo Server 清除设置的 cookie
- ocaml - 取消分配大数组
- python - 在 python 中,当我尝试选择随机数时,如何确保使用 randint 的种子不断变化?
- php - Fat Free 使用 Google OAuth + Google API
- c# - 使用占位符和 html 标签存储字符串
- python - 使用加密的 SearchField 作为自定义用户模型 (Django) 的键时出错:AttributeError: 'NoneType' object has no attribute '_meta'
- python - Pygame FPS 玩家移动问题(Python)