如何消除TSQL NULL字段(How to eliminate NULL fields in TS

2019-07-30 02:33发布

我开发SQL Server 2008 R2的TSQL查询。 我想开发这个查询来识别一个记录/客户端。 因为一些值是NULL,我目前在做LEFT上大部分表的连接。 但与LEFT JOIN的问题是,现在我得到> 1点的记录某些客户端。

但是,如果我改变了为内部连接,然后一些客户端完全排除,因为他们对这些列NULL值。 如何限制查询结果只有一个记录/客户端,无论NULL值? 如果有非NULL值,那么我希望它选择与非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    ethnicity_desc  religion
9CD932F1-6BE1-4F80-AB81-0CE32C565BCF    Atreides Foster Home 1  Atreides1               1   Yes Manchester, NH  Gulf Atlantic   Atreides1   00000007                Treatment Foster Home   4042 Arrakis Avenue, Springfield, VT  05156 Open/Re-opened  2011-06-01 00:00:00.000 NULL    NULL    NULL
DCE354D5-A7CC-409F-B5A3-89BF664B7718    Averitte, Leon and Sandra   00000044                1   Yes Birmingham, AL  Gulf Atlantic   AL Averitte, Leon and Sandra    00000044                Treatment Foster Home   3816 5th Avenue, Bessemer, AL  35020, (205)482-4307 Open/Re-opened  2011-08-05 00:00:00.000 NULL    NULL    NULL
DCE354D5-A7CC-409F-B5A3-89BF664B7718    Averitte, Leon and Sandra   00000044                1   Yes Birmingham, AL  Gulf Atlantic   AL Averitte, Leon and Sandra    00000044                Treatment Foster Home   3816 5th Avenue, Bessemer, AL  35020, (205)482-4307 Open/Re-opened  2011-08-05 00:00:00.000 Caucasian/White Non Hispanic    NULL
AD02A43C-6F38-4F35-8C9E-E12422690BFB    Bass, Matthew and Sarah 00000076                1   Yes Jacks    on, MS Central Gulf Coast  MS Bass, Matthew and Sarah  00000076                Treatment Foster Home   506 Eagelwood Drive, Florence, MS  39073, (601)665-7169 Open/Re-opened  2011-04-01 00:00:00.000 NULL    NULL    NULL
AD02A43C-6F38-4F35-8C9E-E12422690BFB    Bass, Matthew and Sarah 00000076                1   Yes Jackson, MS Central Gulf Coast  MS Bass, Matthew and Sarah  00000076                Treatment Foster Home   506 Eagelwood Drive, Florence, MS  39073, (601)665-7169 Open/Re-opened  2011-04-01 00:00:00.000 Caucasian/White NULL    Baptist

你可以看到,无论Averitte和低音配置文件名称与NULL种族,民族,宗教的一个记录。 如何消除这些行(行2和4)?

这里是我的查询目前:

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,
        ethnicity.description as ethnicity_desc,
        religion.description as religion
from  group_profile gp With (NoLock)
    --Office Information
        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 group_profile_race gpr with (nolock) on gpr.race_info_id = race.race_info_id
    left join race_info ri with (nolock) on ri.race_info_id = gpr.race_info_id
    left join ethnicity With(NoLock) On client.ethnicity = ethnicity.ethnicity_id
    left join religion on client.religion = religion.religion_id

Answer 1:

尝试通过group_profile_id分组和选择MAX()每隔一列的。 MAX将选择非空值(如果存在的话,或最大如果多个存在的话)每个重复条目的。 虽然更有效的解决方案将涉及在数据整理出附加NULL的行,从第一一目了然。



文章来源: How to eliminate NULL fields in TSQL