消除SQL查询中的NULL行

时间:2012-06-23 23:39:44

标签: tsql null ssms

  

可能重复:
  How to eliminate NULL fields in TSQL

我正在使用SSMS 2008 R2并正在开发TSQL查询。我只想要1个记录/ profile_name。因为其中一些值为NULL,所以我目前在大多数表上进行LEFT JOINS。但LEFT JOIN的问题是现在我得到>一些profile_names的1条记录!

但是如果我将其更改为INNER JOIN,则会完全排除某些profile_names,因为它们具有这些列的NULL值。无论NULL值如何,我如何将查询结果限制为仅一个记录/ profile_name?如果有非NULL值,那么我希望它选择具有非NULL值的记录。这是初始查询:

select distinct
        gp.group_profile_id,
        gp.profile_name,
        gp.license_number,
        gp.is_accepting,
        case when gp.is_accepting = 1 then 'Yes'
            when gp.is_accepting = 0 then 'No '
            end as is_accepting_placement,
        mo.profile_name as managing_office,
        regions.[region_description] as region,     
        pv.vendor_name,
        pv.id as vendor_id,
        at.description as applicant_type,
        dbo.GetGroupAddress(gp.group_profile_id, null, 0) as [Office Address],
        gsv.status_description
from  group_profile gp With (NoLock)
    inner join group_profile_type gpt With (NoLock) on gp.group_profile_type_id = gpt.group_profile_type_id and gpt.type_code = 'FOSTERHOME' and gp.agency_id = @agency_id and gp.is_deleted = 0
    inner join group_profile mo With (NoLock) on gp.managing_office_id = mo.group_profile_id
    left outer join payor_vendor pv With (NoLock) on gp.payor_vendor_id = pv.payor_vendor_id
    left outer join applicant_type at With (NoLock) on gp.applicant_type_id = at.applicant_type_id and at.is_foster_home = 1
    inner join group_status_view gsv With (NoLock) on gp.group_profile_id = gsv.group_profile_id and gsv.status_value = 'OPEN' and gsv.effective_date =  
    (Select max(b.effective_date) from  group_status_view b  With (NoLock)
    where gp.group_profile_id = b.group_profile_id)
    left outer join regions With (NoLock) on isnull(mo.regions_id, gp.regions_id) = regions.regions_id
    left join enrollment en on en.group_profile_id = gp.group_profile_id
    join event_log el on el.event_log_id = en.event_log_id
    left join people client on client.people_id = el.people_id

如您所见,上述查询的结果是1行/ profile_name:

group_profile_id    profile_name    license_number  is_accepting    is_accepting_placement  managing_office region  vendor_name vendor_id   applicant_type  Office Address  status_description  Cert Date2

但是现在看一下当我添加2个LEFT JOIN和1个附加列时会发生什么:

select distinct
        gp.group_profile_id,
        gp.profile_name,
        gp.license_number,
        gp.is_accepting,
        case when gp.is_accepting = 1 then 'Yes'
             when gp.is_accepting = 0 then 'No '
            end as is_accepting_placement,
        mo.profile_name as managing_office,
        regions.[region_description] as region,     
        pv.vendor_name,
        pv.id as vendor_id,
        at.description as applicant_type,
        dbo.GetGroupAddress(gp.group_profile_id, null, 0) as [Office Address],
        gsv.status_description,
            ri.[description] as race
from  group_profile gp With (NoLock)
    inner join group_profile_type gpt With (NoLock) on gp.group_profile_type_id = gpt.group_profile_type_id and gpt.type_code = 'FOSTERHOME' and gp.agency_id = @agency_id and gp.is_deleted = 0
    inner join group_profile mo With (NoLock) on gp.managing_office_id = mo.group_profile_id
    left outer join payor_vendor pv With (NoLock) on gp.payor_vendor_id = pv.payor_vendor_id
    left outer join applicant_type at With (NoLock) on gp.applicant_type_id = at.applicant_type_id and at.is_foster_home = 1
    inner join group_status_view gsv With (NoLock) on gp.group_profile_id = gsv.group_profile_id and gsv.status_value = 'OPEN' and gsv.effective_date =  
    (Select max(b.effective_date) from  group_status_view b  With (NoLock)
    where gp.group_profile_id = b.group_profile_id)
    left outer join regions With (NoLock) on isnull(mo.regions_id, gp.regions_id) = regions.regions_id
    left join enrollment en on en.group_profile_id = gp.group_profile_id
    join event_log el on el.event_log_id = en.event_log_id
    left join people client on client.people_id = el.people_id
    left join race With (NoLock) on el.people_id = race.people_id
    left join race_info ri with (nolock) on ri.race_info_id = race.race_info_id

上面的查询会产生所有相同的profile_names,但有些具有NULL种族值:

group_profile_id    profile_name    license_number  is_accepting    is_accepting_placement  managing_office region  vendor_name vendor_id   applicant_type  Office Address  status_description  Cert Date2  race

不幸的是,这使得我需要为这一个额外的字段值(种族)加入2个额外的表格而变得复杂。如果我只是将上面的最后两个LEFT JOIN更改为INNER JOIN,那么我将消除上面的NULL行。但我也删除了一些profile_names:

group_profile_id    profile_name    license_number  is_accepting    is_accepting_placement  managing_office region  vendor_name vendor_id   applicant_type  Office Address  status_description  Cert Date2  race

希望我已经提供了这个问题所需的所有细节。

1 个答案:

答案 0 :(得分:1)

不是最优雅的解决方案,而是一种可行的解决方案:

select [stuff]
from  group_profile gp With (NoLock) 
  inner join group_profile_type gpt With (NoLock) on gp.group_profile_type_id = gpt.group_profile_type_id and gpt.type_code = 'FOSTERHOME' and gp.agency_id = @agency_id and gp.is_deleted = 0 
  inner join group_profile mo With (NoLock) on gp.managing_office_id = mo.group_profile_id 
  join payor_vendor pv on ISNULL(gp.payor_vendor_id, 'THISVALUEWILLNEVEROCCUR') = ISNULL(pv.payor_vendor_id, 'THISVALUEWILLNEVEROCCUR')
...etc...

我发布的最大问题是你将进行大量的表扫描。