2012-06-22 28 views
0

我正在爲SQL Server 2008 R2開發TSQL查詢。我正在試圖開發這個查詢來識別一個記錄/客戶端。由於其中一些值爲NULL,因此我目前在大多數表上執行了LEFT JOINS。但是LEFT JOINs的問題是,現在我爲一些客戶獲得了> 1的記錄。如何消除TSQL中的NULL字段

但是,如果我將其更改爲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 

你可以看到,無論Averitte和低音配置文件名稱與NULL種族,民族,宗教的一個記錄。我如何消除這些行(第2和第4行)?

這是目前我的查詢:每隔一列由group_profile_id

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 
+0

骯髒的方式是將整個事情包裝在一個外部查詢中,只是將WHERE刪除,這是一個很強大的查詢 - 但需要一些'看着它'的時間:) – Charleh

+1

但我必須補充的是 - 只有你得到一個失敗的LEFT JOIN的單個結果,所以你得到行+ NULLS的原因是因爲你的聯接不夠有選擇性 - 例如http://sqlfiddle.com/#!3/971cc/1 – Charleh

+0

謝謝,但我更喜歡更優雅的解決方案 – salvationishere

回答

0

嘗試分組和選擇MAX()。 MAX將選擇每個重複條目的非空值(如果存在的話,或者存在多個的最大值)。 雖然更高效的解決方案將涉及從數據中剔除額外的NULL行,乍一看。