选择列IF条件,否则返回列

时间:2013-08-05 13:50:53

标签: sql

嗨,我有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语句时我必须查看条件并返回列,对此可以有任何帮助。

2 个答案:

答案 0 :(得分:1)

这是您考虑的另一个选择

SELECT NM1.nm101
     , CLP.Segment_GUID as CLPSegmentGuid
     , CASE NM1.nm101
         WHEN 'IL' THEN 'INSURED'
         WHEN '74' THEN 'Corrected'
         WHEN '82' THEN 'Crossover'
       END As type       
     , 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 As NM1 
 INNER
  JOIN X12_CLP As CLP
    ON CLP.segment_guid = NM1.parent_segment_guid
WHERE  NM1.nm101 IN ('IL', '74', '82')

答案 1 :(得分:0)

不,您无法在查询中间更改输出列。一种选择是返回所有列并根据您的条件填充它们:

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

它并不漂亮,但如果您在一个结果集中需要所有这些结果,那么它可能是最佳选择。我的偏好是使用更通用的列名,让消费者根据适当的条件决定调用它的名称:

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