我有大桌子,有大约200个字段。从中创建维度表时,大约100个字段将映射到一个字段。问题是我必须像100个插入语句一样逐个添加这100个字段的值。有没有像循环或其他东西,我可以更有效地实现这一目标?
以下是代码示例:
insert into DimTableA(visit_no, patient, facility, icd9, icd9_flag, ip_op)
select D.registration, D3.med_number, D3.Hosp_Id, D.final_diagnosis_18,'d',IF(D3.Admit_Type in(1,2,3),'Inpatient','Outpatient'),
from svch_dischs3_s D3, svch_diags_s D
insert into DimTableA(visit_no, patient, facility, icd9, icd9_flag, ip_op)
select D.registration, D3.med_number, D3.Hosp_Id, D.final_diagnosis_19,'d',IF(D3.Admit_Type in(1,2,3),'Inpatient','Outpatient'),
from svch_dischs3_s D3, svch_diags_s D
insert into DimTableA(visit_no, patient, facility, icd9, icd9_flag, ip_op)
select D.registration, D3.med_number, D3.Hosp_Id, D.final_diagnosis_20,'d',IF(D3.Admit_Type in(1,2,3),'Inpatient','Outpatient'),
from svch_dischs3_s D3, svch_diags_s D;
.........等等
唯一更改的字段名称是'icd9'输入(即D.final_diagnosis_18,final_diagnosis_19,final_diagnosis_20 ....)
任何帮助都会非常感激,小伙子们。 :)
答案 0 :(得分:1)
您可以使用交叉连接将这些列展开到一组固定的数字,如下所示:
insert into DimTableA (
visit_no,
patient,
facility,
icd9,
icd9_flag,
ip_op
)
select
D.registration,
D3.med_number,
D3.Hosp_Id,
case N.number
when 1 then D.final_diagnosis_18
when 2 then D.final_diagnosis_19
when 3 then D.final_diagnosis_20
...
end,
'd',
IF(D3.Admit_Type in(1,2,3),'Inpatient','Outpatient')
from
svch_dischs3_s D3,
svch_diags_s D,
(
select 1 as number union all
select 2 union all
select 3 union all
... /* up to the number of columns to unpivot */
) N
您可以create and populate a persistent numbers table而不是内联视图,并在查询中使用该表的必要子集。在这种情况下,查询会像这样改变
...
from
svch_dischs3_s D3,
svch_diags_s D,
numbers N
where N.number between 1 and … /* the number of columns to unpivot */
答案 1 :(得分:0)
您可以尝试在子选择中使用UNION
运行单个INSERT
。
你当前的SQL中有一个Cartesian join,所以我猜你错过了某个地方的连接。无论如何,这应该让你知道我在驾驶什么:
insert into DimTableA(visit_no, patient, facility, icd9, icd9_flag, ip_op)
select D.registration, D3.med_number, D3.Hosp_Id, D.final_diagnosis_18,'d',IF(D3.Admit_Type in(1,2,3),'Inpatient','Outpatient')
from svch_dischs3_s D3, svch_diags_s D
-- where D3.???? = D.????
union
select D.registration, D3.med_number, D3.Hosp_Id, D.final_diagnosis_19,'d',IF(D3.Admit_Type in(1,2,3),'Inpatient','Outpatient')
from svch_dischs3_s D3, svch_diags_s D
-- where D3.???? = D.????
union
select D.registration, D3.med_number, D3.Hosp_Id, D.final_diagnosis_20,'d',IF(D3.Admit_Type in(1,2,3),'Inpatient','Outpatient')
from svch_dischs3_s D3, svch_diags_s D
-- where D3.???? = D.????;
这是一个更理论化的例子:
create table dimTable
(id int unsigned not null default 0,
field varchar(50)
);
create table sourceTable
(id int unsigned not null default 0,
field1 varchar(50),
field2 varchar(50),
field3 varchar(50),
field4 varchar(50),
field5 varchar(50)
);
insert into sourceTable (id,field1,field2,field3,field4,field5) values (1,"hello","these","are","some","values");
insert into sourceTable (id,field1,field2,field3,field4,field5) values (2,"hello2","these2","are2","some2","values2");
insert into sourceTable (id,field1,field2,field3,field4,field5) values (3,"hello3","these3","are3","some3","values3");
insert into sourceTable (id,field1,field2,field3,field4,field5) values (4,"hello4","these4","are4","some4","values4");
insert into sourceTable (id,field1,field2,field3,field4,field5) values (5,"hello5","these5","are5","some5","values5");
insert into dimTable (id, field)
select id,field1 from sourceTable
union
select id,field2 from sourceTable
union
select id,field3 from sourceTable
union
select id,field4 from sourceTable
union
select id,field5 from sourceTable;
select *
from dimTable;
希望它有所帮助!
答案 2 :(得分:0)
存储过程可以解决您的问题。您可能只需要将要插入的字段值和后缀传递给D.final_diagnosis_
字段。
示例代码段如下所示:
drop procedure if exists proc_insert_icd9;
delimiter //
create procedure proc_insert_icd9( final_diagnosis_suffix int )
begin
declare suffixId int;
declare sql_insert_str varchar( 255 );
declare sql_select_str varchar( 255 );
declare sql_temp varchar( 255 );
set suffixId = final_diagnosis_suffix;
set sql_insert_str = 'insert into DimTableA(visit_no, patient, facility, icd9, icd9_flag, ip_op) ';
set sql_select_str = concat( 'select D.registration, D3.med_number, D3.Hosp_Id, D.final_diagnosis_', suffixId, ', ''d'', IF(D3.Admit_Type in(1,2,3), ''Inpatient'', ''Outpatient'' ) from svch_dischs3_s D3, svch_diags_s D' );
select concat( sql_insert_str, sql_select_str ) into @sql_temp;
prepare stmt from @sql_temp;
execute stmt;
end;
//
delimiter ;
现在尝试使用final_diagnosis
字段的正确后缀值作为参数调用您需要多次的过程。
示例:
call proc_insert_icd9( 18 );
call proc_insert_icd9( 19 );
注意:您可以修改程序以包含