SQL - 查询中的IF语句

时间:2017-10-11 16:38:28

标签: sql sql-server ssms

我希望在下面的代码中添加一个IF语句,该代码将检查来自Prestige的" Medicaid#是否属于" [Medicaid#from Prestige]"细胞

如果找到了,我想让它显示" TRUE"如果不是那么" FALSE"我不确定如何在代码本身内执行此操作。我确定我忽略了一些东西,但是已经查看了代码,并且在我的代码需要的情况下,我们无法找到一种确切的方法来提取代码。

SELECT DISTINCT
         pml.NPI AS [PML NPI] -- COALESCE will get first non null value
    ,pml.[Provider Name] as [Prov. Name from PML]
    ,pp.npi as [Prestige NPI]
    ,pp.last_name as [Last name (Prestige)]
    ,pp.first_name as [First name (Prestige)]
    ,case
        when pp.provider_type = 'i' then 'Individual'
      when pp.provider_type = 'g' then 'Group'
        else 'UNDEFINED'
    end as [Provider type]
    ,pp.Group_name as [Group name (Prestige)]
    ,case
        when pml.[Current Medicaid Enrollment Status A Active I Inactive E Ineligible] = 'A' then 'Active'
         when pml.[Current Medicaid Enrollment Status A Active I Inactive E Ineligible] = 'I' then 'Inactive'
      when pml.[Current Medicaid Enrollment Status A Active I Inactive E Ineligible] = 'E' then 'Ineligible'
        else 'UNDEFINED'
    end as [Medicaid Status]
    ,pml.[Florida Medicaid Provider ID] as [Medicaid # from PML]
    ,pp.medicaid_number as [Medicaid # from Prestige]
    ,case 
      when pml.[Florida Medicaid Provider ID]  like '*'+pp.medicaid_number+'*' then 'TRUE'
      else 'FALSE'
    end as [Medicaid match?]

    FROM [DWH].[dbo].[PML] as pml -- Full join shows all records, null will show for missing records
        FULL JOIN [DWH].[dbo].[PrestigeProviders] as pp  on pml.NPI = pp.npi
    WHERE (pml.npi IS not NULL AND pp.npi IS not NULL) -- only show when one side of the join fails

调整我的代码,并获取下表(截断相关数据并防止HIPAA违规):

Medicaid # from PML   Medicaid # from Prestige   Medicaid match?
18739600              018739600                  FALSE
13104200              013104200                  FALSE
9851900               009851900                  FALSE
10331600              009851900                  FALSE

1 个答案:

答案 0 :(得分:1)

  1. LIKE的通配符为%,而不是*
  2. 标准SQL中的串联符号为||而非+。但它是SQL Server中的+
  3. 应该是' 009851900'喜欢'%9851900%',不是' 9851900'喜欢'%009851900%'。
  4. 因此:

    case when pp.medicaid_number like '%' + pml.[Florida Medicaid Provider ID] + '%' 
      then 'TRUE'
      else 'FALSE'
    end as [Medicaid match?]