2012-07-03 72 views
2

我有大的表,大約200個字段。當從其中創建維表時,大約100個這些字段將被映射到一個字段。問題是我必須像100個插入語句一樣逐個添加這100個字段的值。有沒有像循環或什麼我可以更有效地實現這一點?在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 

    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

你可以使用交叉連接到這樣的固定組數字unpivot的那些列:

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 */ 
0

您可以嘗試在子選擇中運行一個INSERTUNION

您當前的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; 

希望它能幫助!

+0

謝謝,湯姆。是的,我錯過了where子句..儘管在我的實際代碼中有。你的例子可以幫助很多,但我正在尋找更有效的空間......像光標或其他東西,你知道我的意思嗎? –

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. where子句到選擇聲明。
相關問題