0

我正在为 SQL Server 2008 R2 开发一个 TSQL 查询。我正在尝试开发此查询以识别一个记录/客户。因为其中一些值是 NULL,所以我目前正在对大多数表进行 LEFT JOINS。但是 LEFT JOIN 的问题是,现在我为某些客户获得了 > 1 条记录。

但是,如果我将其更改为 INNER JOIN,则某些客户端将被完全排除,因为它们对这些列具有 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

您可以看到 Averitt 和 Bass 的个人资料名称都有一条记录,其中种族、民族、宗教为 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
4

1 回答 1

0

尝试按 group_profile_id 分组并选择每隔一列的 MAX() 。MAX 将选择每个重复条目的非空值(如果存在一个,或者如果存在多个,则为最大值)。虽然更有效的解决方案将涉及整理数据中额外的 NULL 行,乍一看。

于 2012-06-25T15:53:24.647 回答