2

我有一张大桌子,大约有 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 ....)

小伙子们,任何帮助将不胜感激。:)

4

3 回答 3

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

您可以创建和填充持久数字表而不是内联视图,并在查询中使用该表的必要子集。在这种情况下,查询会像这样改变

...
from
  svch_dischs3_s D3,
  svch_diags_s D,
  numbers N
where N.number between 1 and … /* the number of columns to unpivot */
于 2012-07-03T13:21:45.720 回答
0

您可以尝试在子选择中运行INSERT带有a 的单曲。UNION

您当前的 SQL 中有一个笛卡尔连接,所以我猜测您在某处缺少连接。无论如何,这应该让您了解我在做什么:

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;

希望能帮助到你!

于 2012-07-03T11:51:27.133 回答
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 );

注意:您可以修改程序以包括

  1. 更多输入参数来处理 where 子句等。
  2. select 语句的 where 子句。
于 2012-07-03T13:30:05.833 回答