T-SQL连接

时间:2015-07-08 18:58:18

标签: sql-server tsql

我需要在SQL中连接以下语句。

我正在加入2个表,MEDINFO表和IMMUNIZE表。

每个拍摄代码日期应该只有一条记录,只要它不是null01/01/1901

输出必须如下所示:

"Admin-" + medinfo_field_31 + ", Manuf-" + medinfo_field_32 + ", Lot-" + medinfo_field_33 + ", Exp-" + medinfo_field_34 + ", Site-" + medifno_field_35 + ", Dose-" + medinfo_field_36

以下是我必须创建的SQL语句的标准:

IF immunize_shot_code = 'FLU', 
   then concatenate medinfo_field_12 and medinfo_field_19 thru medinfo_field_24

IF immunize_shot_code = 'MENI', 
   then concatenate medinof_field_11 and medinfo_field_25 thru medinof_field_30

IF immunize_shot_code = 'TETA', 
   then concatenate medinfo_field_13 thru medinfo_field_18 and medinfo_field_31 thru medinfo_field_36"

感谢您的任何想法。

2 个答案:

答案 0 :(得分:0)

这看起来像是跟随:

SELECT 

    (CASE WHEN Immunize_shot_code='Flu'
          THEN medinfo_field_12+', '+med_info_field_19+ ', '+med_info_field_20+', '+med_info_field_21+ ', '+med_info_field_22+', '+med_info_field_23+', 'med_info_field_24
         WHEN immunize_shot_code='MENI'
           THEN [CONCATINATE AS PER ABOVE]
        .....
        .....
        END) as ImmuCode
FROM TABLE_1

答案 1 :(得分:0)

就像上面提到的其中一些,你可以使用select case when i.immunize_shot_Code = 'FLU' then concat(m.medinfo_field_12,',',m.medinfo_field_19,',',m.medinfo_field_24...) when i.immunize_Shot_Code = 'MENI' then concat(m.medinfo_field_11,',',m.medinfo_field_11) when i.immunize_Shot_code = 'TETA' then concat(m.medinfo_field_13,',',m.medinfo_field18,',') end as concat_value from medinfo m join immunize i on m.id = i.medinfo_id --change this to match your join where (shot_code_date is not null or shot_code_date <> '01-01-1901'); 这样的语句:

{{1}}