-1

こんにちは、1列の条件があり、次のようになります

 select    
 WHEN NM1.nm101 = 'IL' THEN ( CLP.Segment_GUID as CLPSegmentGuid,
                              NM1.NM102 as [INSURED_Entity_Type_Qualifier],
                              NM1.NM103 as [INSURED_Entity_Last_Name],
                              NM1.NM104 as [INSURED_Entity_First_Name],
                              NM1.NM105 as [INSURED_Entity_Middle_Name],
                              NM1.NM108 as [INSURED_Entity_Identification_Code_Type],
                              NM1.NM109 as [INSURED_Entity_Identification_Code])



 WHEN NM1.nm101 = '74' THEN ( CLP.Segment_GUID as CLPSegmentGuid,
                              NM1.NM102 as [Corrected_Entity_Type_Qualifier],
                              NM1.NM103 as [Corrected_Entity_Last_Name],
                              NM1.NM104 as [Corrected_Entity_First_Name],
                              NM1.NM105 as [Corrected_Entity_Middle_Name],
                              NM1.NM108 as [Corrected_Entity_Identification_Code_Type],
                              NM1.NM109 as [Corrected_Entity_Identification_Code])



ELSE NM1.nm101 = '82' THEN ( CLP.Segment_GUID as CLPSegmentGuid,
                              NM1.NM102 as [CROSSOVER_Entity_Type_Qualifier],
                              NM1.NM103 as [CROSSOVER_Entity_Last_Name],
                              NM1.NM104 as [CROSSOVER_Entity_First_Name],
                              NM1.NM105 as [CROSSOVER_Entity_Middle_Name],
                              NM1.NM108 as [CROSSOVER_Entity_Identification_Code_Type],
                              NM1.NM109 as [CROSSOVER_Entity_Identification_Code])



          From  X12_NM1 NM1 
          Inner Join X12_CLP CLP
          ON CLP.segment_guid = NM1.parent_segment_guid

そのため、その特定の列 NM101 に対して select ステートメントを実行しているときに、条件を確認して列を返す必要があります。

4

2 に答える 2

0

いいえ、クエリの途中で出力列を変更することはできません。1 つのオプションは、すべての列を返し、条件に基づいて入力することです。

select    
     CLP.Segment_GUID as CLPSegmentGuid,
     CASE WHEN NM1.nm101 = 'IL' THEN NM1.NM102 ELSE NULL as [INSURED_Entity_Type_Qualifier],
     CASE WHEN NM1.nm101 = 'IL' THEN NM1.NM103 ELSE NULL as [INSURED_Entity_Last_Name],
     CASE WHEN NM1.nm101 = 'IL' THEN NM1.NM104 ELSE NULL as [INSURED_Entity_First_Name],
     CASE WHEN NM1.nm101 = 'IL' THEN NM1.NM105 ELSE NULL as [INSURED_Entity_Middle_Name],
     CASE WHEN NM1.nm101 = 'IL' THEN NM1.NM108 ELSE NULL as [INSURED_Entity_Identification_Code_Type],
     CASE WHEN NM1.nm101 = 'IL' THEN NM1.NM109 ELSE NULL as [INSURED_Entity_Identification_Code],

     CASE WHEN NM1.nm101 = '74' THEN NM1.NM102 ELSE NULL as [Corrected_Entity_Type_Qualifier],
     CASE WHEN NM1.nm101 = '74' THEN NM1.NM103 ELSE NULL as [Corrected_Entity_Last_Name],
     CASE WHEN NM1.nm101 = '74' THEN NM1.NM104 ELSE NULL as [Corrected_Entity_First_Name],
     CASE WHEN NM1.nm101 = '74' THEN NM1.NM105 ELSE NULL as [Corrected_Entity_Middle_Name],
     CASE WHEN NM1.nm101 = '74' THEN NM1.NM108 ELSE NULL as [Corrected_Entity_Identification_Code_Type],
     CASE WHEN NM1.nm101 = '74' THEN NM1.NM109 ELSE NULL as [Corrected_Entity_Identification_Code],

     CASE WHEN NM1.nm101 = '82' THEN NM1.NM102 ELSE NULL as [CROSSOVER_Entity_Type_Qualifier],
     CASE WHEN NM1.nm101 = '82' THEN NM1.NM103 ELSE NULL as [CROSSOVER_Entity_Last_Name],
     CASE WHEN NM1.nm101 = '82' THEN NM1.NM104 ELSE NULL as [CROSSOVER_Entity_First_Name],
     CASE WHEN NM1.nm101 = '82' THEN NM1.NM105 ELSE NULL as [CROSSOVER_Entity_Middle_Name],
     CASE WHEN NM1.nm101 = '82' THEN NM1.NM108 ELSE NULL as [CROSSOVER_Entity_Identification_Code_Type],
     CASE WHEN NM1.nm101 = '82' THEN NM1.NM109 ELSE NULL as [CROSSOVER_Entity_Identification_Code]

From  X12_NM1 NM1 
Inner Join X12_CLP CLP
ON CLP.segment_guid = NM1.parent_segment_guid

きれいではありませんが、これらすべての結果を 1 つの結果セットに含める必要がある場合は、最適なオプションです。私の好みは、より一般的な列名を使用し、消費者が適切な条件に基づいてそれを何と呼ぶか​​を決定できるようにすることです。

select    
     CLP.Segment_GUID as CLPSegmentGuid,
     CASE 
         WHEN NM1.nm101 = 'IL' THEN 'INSURED'
         WHEN NM1.nm101 = '74' THEN 'Corrected'
         WHEN NM1.nm101 = '82' THEN 'CROSSOVER'
         ELSE 'Unknown'
     END as Category,
     NM1.NM102 as [Entity_Type_Qualifier],
     NM1.NM103 as [Entity_Last_Name],
     NM1.NM104 as [Entity_First_Name],
     NM1.NM105 as [Entity_Middle_Name],
     NM1.NM108 as [Entity_Identification_Code_Type],
     NM1.NM109 as [Entity_Identification_Code]
From  X12_NM1 NM1 
Inner Join X12_CLP CLP
ON CLP.segment_guid = NM1.parent_segment_guid
于 2013-08-05T13:58:46.173 に答える