2013-08-21 45 views
0
SELECT NM1.nm101, 
    CLP.Segment_GUID as CLPSegmentGuid, 
    CASE NM1.nm101 
    WHEN 'QC' THEN 'Patient' 
     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 and NM1.Loop_Id ='2100' 

這裏if nm1.nm108 = MI然後我想要那些MI的結果集。關於如果否則在T-SQL

+1

只是爲了澄清 - 你想從您發佈的查詢中排除nm108不等於'MI'的行嗎? – Dan

+0

CASP LOCK ALERT !!!!!!! –

+1

@SonerGönül出於某種原因,SQL程序員傾向於將它們的標題寫入全部大寫。 – 2013-08-21 16:44:29

回答

1

如果我理解你的問題正確,那麼你正在尋找的記錄,其中只有nm1.nm108 = 'MI'則否存在,你想要的那些記錄排除

嘗試是這樣的

IF EXISTS(SELECT 1 FROM X12_NM1 As NM1 
        INNER JOIN X12_CLP As CLP 
         ON CLP.segment_guid = NM1.parent_segment_guid 
         AND NM1.Loop_Id ='2100' 
        WHERE nm1.nm108 = 'MI') 
BEGIN 
SELECT NM1.nm101, 
CLP.Segment_GUID as CLPSegmentGuid 
, CASE NM1.nm101 
     WHEN 'QC' THEN 'Patient' 
    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 
    AND NM1.Loop_Id ='2100' 
WHERE nm1.nm108 = 'MI' 
END 
ELSE 
BEGIN 
SELECT NM1.nm101, 
CLP.Segment_GUID as CLPSegmentGuid 
, CASE NM1.nm101 
     WHEN 'QC' THEN 'Patient' 
    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 
    AND NM1.Loop_Id ='2100' 
WHERE nm1.nm108 <> 'MI' 
END